<!DOCTYPE html><html lang="en"><head><link rel="stylesheet" href="./style.css"><title>Array - 30 seconds of code</title><meta charset="utf-8"><meta name="description" content="Curated collection of useful Javascript snippets that you can understand in 30 seconds or less."><meta name="keywords" content="javascript, snippets, code, programming"><meta name="author" content="Angelos Chalaris (chalarangelo@gmail.com)"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="theme-color" content="#111"><meta property="og:title" content="30 seconds of code"><meta property="og:description" content="Curated collection of useful Javascript snippets that you can understand in 30 seconds or less."/><meta property="og:type" content="website"/><meta property="og:image" content="https://30secondsofcode.org/logos/logo_512.png"><link rel="icon" type="image/png" href="./logos/logo_128.png"><link rel="manifest" href="manifest.json"><script>const search = (node) => {
        if(node.value.toLowerCase().trim() === ''){
          document.querySelector('nav').querySelectorAll('li > a').forEach(x => x.style.display = ''); 
          document.querySelector('nav').querySelectorAll('h4:not(.static-link)').forEach(x => x.classList = 'collapse');
          return;
        }
        let matchingTags = [];
        Array.from(document.querySelector('nav').querySelectorAll('li')).forEach(x => {
          let data = [x.textContent.toLowerCase(), ...x.querySelector('a').getAttribute('tags').split(',')].map(v => !!(v.indexOf(node.value.toLowerCase()) + 1));
          if(data.includes(true)){
            x.style.display = '';
            matchingTags.push(x.querySelector('a').getAttribute('tags').split(',')[0]);
          }
          else x.style.display = 'none';
        });
        Array.from(document.querySelector('nav').querySelectorAll('h4:not(.static-link)')).forEach(x => {
          x.classList = matchingTags.includes(x.textContent.toLowerCase()) ? 'collapse toggled' : 'collapse';
        })
      }
      function scrollToTop(){
        const c = document.querySelector('.card-container').scrollTop;
        if (c > 0) {
          window.requestAnimationFrame(scrollToTop);
          document.querySelector('.card-container').scrollTo(0, c - c / 4);
        }
      };
      function scrollTo(element, to, id, duration) {
        if (duration <= 0) return;
        var difference = to - element.scrollTop;
        var perTick = difference / duration * 40;

        setTimeout(function() {
            element.scrollTop = element.scrollTop + perTick;
            if (element.scrollTop === to) {
              window.location.href = "#"+id;
              return;
            }
            scrollTo(element, to, id, duration - 40);
        }, 40);
      };
      function loader() {
        registerClickListener();
        if(window.innerWidth >= '768')
          document.querySelector('nav').scrollTop = Array.from(document.querySelectorAll('h4')).filter(v => v.innerHTML === 'Array')[0].offsetTop;
        else
          document.querySelector('nav').scrollTop = Array.from(document.querySelectorAll('h4')).filter(v => v.innerHTML === 'Array')[0].offsetTop;
      }
      function registerClickListener() {
        document.addEventListener('click', function (event) {
          if ( event.target.classList.contains('collapse') ) {
            event.target.classList = event.target.classList.contains('toggled') ? 'collapse' : 'collapse toggled';
          }
          else if (event.target.classList.contains('menu-button')) {
            document.querySelector('nav').classList = event.target.classList.contains('toggled') ? '' : 'col-nav';
            document.querySelector('[type="search"]').classList = event.target.classList.contains('toggled') ? '' : 'col-nav';
            event.target.classList = event.target.classList.contains('toggled') ? 'menu-button' : 'menu-button toggled';
          }
          else if (!document.querySelector('nav').contains(event.target) && !document.querySelector('[type="search"]').contains(event.target) && window.innerWidth < '768') {
            document.querySelector('nav').classList = '';
            document.querySelector('[type="search"]').classList = '';
            document.querySelector('.menu-button').classList = 'menu-button';
          }
          else if (event.target.classList.contains('social')){
            if (event.target.classList.contains('fb')){
              window.location = "https://www.facebook.com/30secondsofcode";
            }
            else if (event.target.classList.contains('instagram')) {
              window.location = "https://www.instagram.com/30secondsofcode";
            }
            else if (event.target.classList.contains('twitter')) {
              window.location = "https://twitter.com/30secondsofcode";
            } 
          }
          else if (event.target.classList.contains('copy-button')) {
            const text = event.target.parentElement.parentElement.querySelector(':not(pre) + pre').textContent;
            const textArea = document.createElement("textarea");
            textArea.value = text.trim();
            document.body.appendChild(textArea);
            textArea.select();
            document.execCommand("Copy");
            document.body.removeChild(textArea);
            let tst = document.createElement('div');
            tst.classList = 'toast';
            tst.innerHTML = 'Snippet copied to clipboard!';
            document.body.appendChild(tst);
            setTimeout(function() {
              tst.style.opacity = 0;
              setTimeout(function() {
                document.body.removeChild(tst);
              },300);
            },1700);
          }
        }, false);
      }</script></head><body onload="loader()" class="card-page"><a href="https://github.com/30-seconds/30-seconds-of-code" class="github-corner" aria-label="View source on Github"><svg width="56" height="56" viewBox="0 0 250 250" style="fill:#009688;color:#fff;position:fixed;top:0;border:0;right:0;z-index:1000" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin:130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><button class="menu-button"></button><header><h1 class="logo"><a href="./index"><img src="https://30secondsofcode.org/logos/logo_256.png" alt="logo"/><span id="title">&nbsp;30 seconds of code</span> <small>Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.</small></a></h1></header><div class="container card-container"><input class="search" type="search" id="searchInput" onkeyup="search(this)" placeholder="Search..." aria-label="Snippet search"><nav><h4 class="collapse">Adapter</h4><ul><li><a tags="adapter,function,intermediate" href="./adapter#ary">ary</a></li><li><a tags="adapter,function,intermediate" href="./adapter#call">call</a></li><li><a tags="adapter,function,array,intermediate" href="./adapter#collectinto">collectInto</a></li><li><a tags="adapter,function,intermediate" href="./adapter#flip">flip</a></li><li><a tags="adapter,function,intermediate" href="./adapter#over">over</a></li><li><a tags="adapter,function,intermediate" href="./adapter#overargs">overArgs</a></li><li><a tags="adapter,function,promise,intermediate" href="./adapter#pipeasyncfunctions">pipeAsyncFunctions</a></li><li><a tags="adapter,function,intermediate" href="./adapter#pipefunctions">pipeFunctions</a></li><li><a tags="adapter,function,promise,intermediate" href="./adapter#promisify">promisify</a></li><li><a tags="adapter,function,intermediate" href="./adapter#rearg">rearg</a></li><li><a tags="adapter,intermediate" href="./adapter#spreadover">spreadOver</a></li><li><a tags="adapter,function,intermediate" href="./adapter#unary">unary</a></li></ul><h4 class="collapse">Array</h4><ul><li><a tags="array,function,beginner" href="#all">all</a></li><li><a tags="array,function,beginner" href="#allequal">allEqual</a></li><li><a tags="array,function,beginner" href="#any">any</a></li><li><a tags="array,string,utility,intermediate" href="#arraytocsv">arrayToCSV</a></li><li><a tags="array,intermediate" href="#bifurcate">bifurcate</a></li><li><a tags="array,function,intermediate" href="#bifurcateby">bifurcateBy</a></li><li><a tags="array,intermediate" href="#chunk">chunk</a></li><li><a tags="array,beginner" href="#compact">compact</a></li><li><a tags="array,object,intermediate" href="#countby">countBy</a></li><li><a tags="array,intermediate" href="#countoccurrences">countOccurrences</a></li><li><a tags="array,recursion,intermediate" href="#deepflatten">deepFlatten</a></li><li><a tags="array,math,beginner" href="#difference">difference</a></li><li><a tags="array,function,intermediate" href="#differenceby">differenceBy</a></li><li><a tags="array,function,intermediate" href="#differencewith">differenceWith</a></li><li><a tags="array,beginner" href="#drop">drop</a></li><li><a tags="array,beginner" href="#dropright">dropRight</a></li><li><a tags="array,function,intermediate" href="#droprightwhile">dropRightWhile</a></li><li><a tags="array,function,intermediate" href="#dropwhile">dropWhile</a></li><li><a tags="array,beginner" href="#everynth">everyNth</a></li><li><a tags="array,beginner" href="#filternonunique">filterNonUnique</a></li><li><a tags="array,function,intermediate" href="#filternonuniqueby">filterNonUniqueBy</a></li><li><a tags="array,beginner" href="#findlast">findLast</a></li><li><a tags="array,function,intermediate" href="#findlastindex">findLastIndex</a></li><li><a tags="array,intermediate" href="#flatten">flatten</a></li><li><a tags="array,function,intermediate" href="#foreachright">forEachRight</a></li><li><a tags="array,object,intermediate" href="#groupby">groupBy</a></li><li><a tags="array,beginner" href="#head">head</a></li><li><a tags="array,intermediate" href="#indexofall">indexOfAll</a></li><li><a tags="array,beginner" href="#initial">initial</a></li><li><a tags="array,intermediate" href="#initialize2darray">initialize2DArray</a></li><li><a tags="array,math,intermediate" href="#initializearraywithrange">initializeArrayWithRange</a></li><li><a tags="array,math,intermediate" href="#initializearraywithrangeright">initializeArrayWithRangeRight</a></li><li><a tags="array,math,intermediate" href="#initializearraywithvalues">initializeArrayWithValues</a></li><li><a tags="array,recursion,intermediate" href="#initializendarray">initializeNDArray</a></li><li><a tags="array,math,intermediate" href="#intersection">intersection</a></li><li><a tags="array,function,intermediate" href="#intersectionby">intersectionBy</a></li><li><a tags="array,function,intermediate" href="#intersectionwith">intersectionWith</a></li><li><a tags="array,intermediate" href="#issorted">isSorted</a></li><li><a tags="array,intermediate" href="#join">join</a></li><li><a tags="array,string,object,advanced" href="#jsontocsv">JSONtoCSV</a></li><li><a tags="array,beginner" href="#last">last</a></li><li><a tags="array,string,utility,intermediate" href="#longestitem">longestItem</a></li><li><a tags="array,object,advanced" href="#mapobject">mapObject</a></li><li><a tags="array,math,beginner" href="#maxn">maxN</a></li><li><a tags="array,math,beginner" href="#minn">minN</a></li><li><a tags="array,function,beginner" href="#none">none</a></li><li><a tags="array,beginner" href="#nthelement">nthElement</a></li><li><a tags="array,beginner" href="#offset">offset</a></li><li><a tags="array,object,function,intermediate" href="#partition">partition</a></li><li><a tags="array,recursion,advanced" href="#permutations">permutations</a></li><li><a tags="array,intermediate" href="#pull">pull</a></li><li><a tags="array,advanced" href="#pullatindex">pullAtIndex</a></li><li><a tags="array,advanced" href="#pullatvalue">pullAtValue</a></li><li><a tags="array,function,advanced" href="#pullby">pullBy</a></li><li><a tags="array,intermediate" href="#reducedfilter">reducedFilter</a></li><li><a tags="array,function,intermediate" href="#reducesuccessive">reduceSuccessive</a></li><li><a tags="array,function,intermediate" href="#reducewhich">reduceWhich</a></li><li><a tags="array,beginner" href="#reject">reject</a></li><li><a tags="array,intermediate" href="#remove">remove</a></li><li><a tags="array,random,beginner" href="#sample">sample</a></li><li><a tags="array,random,intermediate" href="#samplesize">sampleSize</a></li><li><a tags="array,intermediate" href="#shank">shank</a></li><li><a tags="array,random,intermediate" href="#shuffle">shuffle</a></li><li><a tags="array,math,beginner" href="#similarity">similarity</a></li><li><a tags="array,math,intermediate" href="#sortedindex">sortedIndex</a></li><li><a tags="array,math,function,intermediate" href="#sortedindexby">sortedIndexBy</a></li><li><a tags="array,math,intermediate" href="#sortedlastindex">sortedLastIndex</a></li><li><a tags="array,math,function,intermediate" href="#sortedlastindexby">sortedLastIndexBy</a></li><li><a tags="array,sort,advanced,intermediate" href="#stablesort">stableSort</a></li><li><a tags="array,math,intermediate" href="#symmetricdifference">symmetricDifference</a></li><li><a tags="array,function,intermediate" href="#symmetricdifferenceby">symmetricDifferenceBy</a></li><li><a tags="array,function,intermediate" href="#symmetricdifferencewith">symmetricDifferenceWith</a></li><li><a tags="array,beginner" href="#tail">tail</a></li><li><a tags="array,beginner" href="#take">take</a></li><li><a tags="array,intermediate" href="#takeright">takeRight</a></li><li><a tags="array,function,intermediate" href="#takerightwhile">takeRightWhile</a></li><li><a tags="array,function,intermediate" href="#takewhile">takeWhile</a></li><li><a tags="array,intermediate" href="#tohash">toHash</a></li><li><a tags="array,math,beginner" href="#union">union</a></li><li><a tags="array,function,intermediate" href="#unionby">unionBy</a></li><li><a tags="array,function,intermediate" href="#unionwith">unionWith</a></li><li><a tags="array,beginner" href="#uniqueelements">uniqueElements</a></li><li><a tags="array,function,intermediate" href="#uniqueelementsby">uniqueElementsBy</a></li><li><a tags="array,function,intermediate" href="#uniqueelementsbyright">uniqueElementsByRight</a></li><li><a tags="array,math,intermediate" href="#uniquesymmetricdifference">uniqueSymmetricDifference</a></li><li><a tags="array,intermediate" href="#unzip">unzip</a></li><li><a tags="array,function,advanced" href="#unzipwith">unzipWith</a></li><li><a tags="array,beginner" href="#without">without</a></li><li><a tags="array,math,intermediate" href="#xprod">xProd</a></li><li><a tags="array,intermediate" href="#zip">zip</a></li><li><a tags="array,object,intermediate" href="#zipobject">zipObject</a></li><li><a tags="array,function,advanced" href="#zipwith">zipWith</a></li></ul><h4 class="collapse">Browser</h4><ul><li><a tags="browser,array,intermediate" href="./browser#arraytohtmllist">arrayToHtmlList</a></li><li><a tags="browser,intermediate" href="./browser#bottomvisible">bottomVisible</a></li><li><a tags="browser,string,advanced" href="./browser#copytoclipboard">copyToClipboard</a></li><li><a tags="browser,advanced" href="./browser#counter">counter</a></li><li><a tags="browser,utility,beginner" href="./browser#createelement">createElement</a></li><li><a tags="browser,event,advanced" href="./browser#createeventhub">createEventHub</a></li><li><a tags="browser,url,beginner" href="./browser#currenturl">currentURL</a></li><li><a tags="browser,intermediate" href="./browser#detectdevicetype">detectDeviceType</a></li><li><a tags="browser,intermediate" href="./browser#elementcontains">elementContains</a></li><li><a tags="browser,advanced" href="./browser#elementisvisibleinviewport">elementIsVisibleInViewport</a></li><li><a tags="browser,intermediate" href="./browser#getscrollposition">getScrollPosition</a></li><li><a tags="browser,css,beginner" href="./browser#getstyle">getStyle</a></li><li><a tags="browser,css,beginner" href="./browser#hasclass">hasClass</a></li><li><a tags="browser,utility,advanced,promise,advanced" href="./browser#hashbrowser">hashBrowser</a></li><li><a tags="browser,css,beginner" href="./browser#hide">hide</a></li><li><a tags="browser,url,intermediate" href="./browser#httpsredirect">httpsRedirect</a></li><li><a tags="browser,beginner" href="./browser#insertafter">insertAfter</a></li><li><a tags="browser,beginner" href="./browser#insertbefore">insertBefore</a></li><li><a tags="browser,beginner" href="./browser#isbrowsertabfocused">isBrowserTabFocused</a></li><li><a tags="browser,array,beginner" href="./browser#nodelisttoarray">nodeListToArray</a></li><li><a tags="browser,event,advanced" href="./browser#observemutations">observeMutations</a></li><li><a tags="browser,event,intermediate" href="./browser#off">off</a></li><li><a tags="browser,event,intermediate" href="./browser#on">on</a></li><li><a tags="browser,event,advanced" href="./browser#onuserinputchange">onUserInputChange</a></li><li><a tags="browser,utility,intermediate" href="./browser#prefix">prefix</a></li><li><a tags="browser,utility,intermediate" href="./browser#recordanimationframes">recordAnimationFrames</a></li><li><a tags="browser,url,beginner" href="./browser#redirect">redirect</a></li><li><a tags="browser,function,advanced,promise,url" href="./browser#runasync">runAsync</a></li><li><a tags="browser,intermediate" href="./browser#scrolltotop">scrollToTop</a></li><li><a tags="browser,beginner" href="./browser#setstyle">setStyle</a></li><li><a tags="browser,css,beginner" href="./browser#show">show</a></li><li><a tags="browser,css,intermediate" href="./browser#smoothscroll">smoothScroll</a></li><li><a tags="browser,beginner" href="./browser#toggleclass">toggleClass</a></li><li><a tags="browser,event,intermediate" href="./browser#triggerevent">triggerEvent</a></li><li><a tags="browser,utility,random,intermediate" href="./browser#uuidgeneratorbrowser">UUIDGeneratorBrowser</a></li></ul><h4 class="collapse">Date</h4><ul><li><a tags="date,beginner" href="./date#dayofyear">dayOfYear</a></li><li><a tags="date,math,string,utility,intermediate" href="./date#formatduration">formatDuration</a></li><li><a tags="date,intermediate" href="./date#getcolontimefromdate">getColonTimeFromDate</a></li><li><a tags="date,intermediate" href="./date#getdaysdiffbetweendates">getDaysDiffBetweenDates</a></li><li><a tags="date,beginner" href="./date#getmeridiemsuffixofinteger">getMeridiemSuffixOfInteger</a></li><li><a tags="date,utility,beginner" href="./date#isafterdate">isAfterDate</a></li><li><a tags="date,utility,beginner" href="./date#isbeforedate">isBeforeDate</a></li><li><a tags="date,utility,beginner" href="./date#issamedate">isSameDate</a></li><li><a tags="date,math,beginner" href="./date#maxdate">maxDate</a></li><li><a tags="date,math,beginner" href="./date#mindate">minDate</a></li><li><a tags="date,intermediate" href="./date#tomorrow">tomorrow</a></li></ul><h4 class="collapse">Function</h4><ul><li><a tags="function,intermediate" href="./function#attempt">attempt</a></li><li><a tags="function,object,intermediate" href="./function#bind">bind</a></li><li><a tags="function,object,intermediate" href="./function#bindkey">bindKey</a></li><li><a tags="function,intermediate" href="./function#chainasync">chainAsync</a></li><li><a tags="function,intermediate" href="./function#compose">compose</a></li><li><a tags="function,intermediate" href="./function#composeright">composeRight</a></li><li><a tags="function,intermediate" href="./function#converge">converge</a></li><li><a tags="function,recursion,intermediate" href="./function#curry">curry</a></li><li><a tags="function,intermediate" href="./function#debounce">debounce</a></li><li><a tags="function,intermediate" href="./function#defer">defer</a></li><li><a tags="function,intermediate" href="./function#delay">delay</a></li><li><a tags="function,utility,beginner" href="./function#functionname">functionName</a></li><li><a tags="function,intermediate" href="./function#hz">hz</a></li><li><a tags="function,advanced" href="./function#memoize">memoize</a></li><li><a tags="function,beginner" href="./function#negate">negate</a></li><li><a tags="function,intermediate" href="./function#once">once</a></li><li><a tags="function,intermediate" href="./function#partial">partial</a></li><li><a tags="function,intermediate" href="./function#partialright">partialRight</a></li><li><a tags="function,promise,intermediate" href="./function#runpromisesinseries">runPromisesInSeries</a></li><li><a tags="function,promise,intermediate" href="./function#sleep">sleep</a></li><li><a tags="function,advanced" href="./function#throttle">throttle</a></li><li><a tags="function,intermediate" href="./function#times">times</a></li><li><a tags="function,intermediate" href="./function#uncurry">uncurry</a></li><li><a tags="function,array,intermediate" href="./function#unfold">unfold</a></li><li><a tags="function,intermediate" href="./function#when">when</a></li></ul><h4 class="collapse">Math</h4><ul><li><a tags="math,beginner" href="./math#approximatelyequal">approximatelyEqual</a></li><li><a tags="math,array,beginner" href="./math#average">average</a></li><li><a tags="math,array,function,intermediate" href="./math#averageby">averageBy</a></li><li><a tags="math,intermediate" href="./math#binomialcoefficient">binomialCoefficient</a></li><li><a tags="math,beginner" href="./math#clampnumber">clampNumber</a></li><li><a tags="math,beginner" href="./math#degreestorads">degreesToRads</a></li><li><a tags="math,array,beginner" href="./math#digitize">digitize</a></li><li><a tags="math,beginner" href="./math#distance">distance</a></li><li><a tags="math,array,advanced" href="./math#elo">elo</a></li><li><a tags="math,recursion,beginner" href="./math#factorial">factorial</a></li><li><a tags="math,array,beginner" href="./math#fibonacci">fibonacci</a></li><li><a tags="math,recursion,beginner" href="./math#gcd">gcd</a></li><li><a tags="math,intermediate" href="./math#geometricprogression">geometricProgression</a></li><li><a tags="math,beginner" href="./math#hammingdistance">hammingDistance</a></li><li><a tags="math,beginner" href="./math#inrange">inRange</a></li><li><a tags="math,beginner" href="./math#isdivisible">isDivisible</a></li><li><a tags="math,beginner" href="./math#iseven">isEven</a></li><li><a tags="math,beginner,intermediate" href="./math#isprime">isPrime</a></li><li><a tags="math,recursion,beginner" href="./math#lcm">lcm</a></li><li><a tags="math,utility,advanced" href="./math#luhncheck">luhnCheck</a></li><li><a tags="math,array,function,beginner" href="./math#maxby">maxBy</a></li><li><a tags="math,array,intermediate" href="./math#median">median</a></li><li><a tags="math,array,function,beginner" href="./math#minby">minBy</a></li><li><a tags="math,intermediate" href="./math#percentile">percentile</a></li><li><a tags="math,beginner" href="./math#powerset">powerset</a></li><li><a tags="math,array,intermediate" href="./math#primes">primes</a></li><li><a tags="math,beginner" href="./math#radstodegrees">radsToDegrees</a></li><li><a tags="math,utility,random,intermediate" href="./math#randomintarrayinrange">randomIntArrayInRange</a></li><li><a tags="math,utility,random,beginner" href="./math#randomintegerinrange">randomIntegerInRange</a></li><li><a tags="math,utility,random,beginner" href="./math#randomnumberinrange">randomNumberInRange</a></li><li><a tags="math,intermediate" href="./math#round">round</a></li><li><a tags="math,utility,intermediate" href="./math#sdbm">sdbm</a></li><li><a tags="math,array,intermediate" href="./math#standarddeviation">standardDeviation</a></li><li><a tags="math,array,beginner" href="./math#sum">sum</a></li><li><a tags="math,array,function,intermediate" href="./math#sumby">sumBy</a></li><li><a tags="math,intermediate" href="./math#sumpower">sumPower</a></li><li><a tags="math,beginner" href="./math#tosafeinteger">toSafeInteger</a></li></ul><h4 class="collapse">Node</h4><ul><li><a tags="node,string,utility,beginner" href="./node#atob">atob</a></li><li><a tags="node,string,utility,beginner" href="./node#btoa">btoa</a></li><li><a tags="node,utility,string,intermediate" href="./node#colorize">colorize</a></li><li><a tags="node,intermediate" href="./node#hasflags">hasFlags</a></li><li><a tags="node,utility,promise,intermediate" href="./node#hashnode">hashNode</a></li><li><a tags="node,type,intermediate" href="./node#isduplexstream">isDuplexStream</a></li><li><a tags="node,type,intermediate" href="./node#isreadablestream">isReadableStream</a></li><li><a tags="node,type,intermediate" href="./node#isstream">isStream</a></li><li><a tags="node,intermediate" href="./node#istravisci">isTravisCI</a></li><li><a tags="node,type,intermediate" href="./node#iswritablestream">isWritableStream</a></li><li><a tags="node,json,intermediate" href="./node#jsontofile">JSONToFile</a></li><li><a tags="node,array,string,beginner" href="./node#readfilelines">readFileLines</a></li><li><a tags="node,string,beginner" href="./node#untildify">untildify</a></li><li><a tags="node,utility,random,intermediate" href="./node#uuidgeneratornode">UUIDGeneratorNode</a></li></ul><h4 class="collapse">Object</h4><ul><li><a tags="object,function,intermediate" href="./object#bindall">bindAll</a></li><li><a tags="object,recursion,intermediate" href="./object#deepclone">deepClone</a></li><li><a tags="object,recursion,intermediate" href="./object#deepfreeze">deepFreeze</a></li><li><a tags="object,intermediate" href="./object#defaults">defaults</a></li><li><a tags="object,recursion,intermediate" href="./object#dig">dig</a></li><li><a tags="object,array,type,advanced" href="./object#equals">equals</a></li><li><a tags="object,function,intermediate" href="./object#findkey">findKey</a></li><li><a tags="object,function,intermediate" href="./object#findlastkey">findLastKey</a></li><li><a tags="object,recursion,intermediate" href="./object#flattenobject">flattenObject</a></li><li><a tags="object,intermediate" href="./object#forown">forOwn</a></li><li><a tags="object,intermediate" href="./object#forownright">forOwnRight</a></li><li><a tags="object,function,intermediate" href="./object#functions">functions</a></li><li><a tags="object,intermediate" href="./object#get">get</a></li><li><a tags="object,function,intermediate" href="./object#invertkeyvalues">invertKeyValues</a></li><li><a tags="object,intermediate" href="./object#lowercasekeys">lowercaseKeys</a></li><li><a tags="object,function,intermediate" href="./object#mapkeys">mapKeys</a></li><li><a tags="object,function,intermediate" href="./object#mapvalues">mapValues</a></li><li><a tags="object,type,intermediate" href="./object#matches">matches</a></li><li><a tags="object,type,function,intermediate" href="./object#matcheswith">matchesWith</a></li><li><a tags="object,array,intermediate" href="./object#merge">merge</a></li><li><a tags="object,intermediate" href="./object#nest">nest</a></li><li><a tags="object,array,beginner" href="./object#objectfrompairs">objectFromPairs</a></li><li><a tags="object,array,beginner" href="./object#objecttopairs">objectToPairs</a></li><li><a tags="object,array,intermediate" href="./object#omit">omit</a></li><li><a tags="object,array,function,intermediate" href="./object#omitby">omitBy</a></li><li><a tags="object,array,intermediate" href="./object#orderby">orderBy</a></li><li><a tags="object,array,intermediate" href="./object#pick">pick</a></li><li><a tags="object,array,function,intermediate" href="./object#pickby">pickBy</a></li><li><a tags="object,intermediate" href="./object#renamekeys">renameKeys</a></li><li><a tags="object,beginner" href="./object#shallowclone">shallowClone</a></li><li><a tags="object,array,string,intermediate" href="./object#size">size</a></li><li><a tags="object,array,intermediate" href="./object#transform">transform</a></li><li><a tags="object,logic,array,intermediate" href="./object#truthcheckcollection">truthCheckCollection</a></li><li><a tags="object,advanced" href="./object#unflattenobject">unflattenObject</a></li></ul><h4 class="collapse">String</h4><ul><li><a tags="string,beginner" href="./string#bytesize">byteSize</a></li><li><a tags="string,array,intermediate" href="./string#capitalize">capitalize</a></li><li><a tags="string,regexp,intermediate" href="./string#capitalizeeveryword">capitalizeEveryWord</a></li><li><a tags="string,array,utility,intermediate" href="./string#csvtoarray">CSVToArray</a></li><li><a tags="string,array,object,advanced" href="./string#csvtojson">CSVToJSON</a></li><li><a tags="string,array,intermediate" href="./string#decapitalize">decapitalize</a></li><li><a tags="string,browser,regexp,intermediate" href="./string#escapehtml">escapeHTML</a></li><li><a tags="string,regexp,intermediate" href="./string#escaperegexp">escapeRegExp</a></li><li><a tags="string,intermediate" href="./string#fromcamelcase">fromCamelCase</a></li><li><a tags="string,utility,beginner" href="./string#indentstring">indentString</a></li><li><a tags="string,utility,browser,url,intermediate" href="./string#isabsoluteurl">isAbsoluteURL</a></li><li><a tags="string,regexp,intermediate" href="./string#isanagram">isAnagram</a></li><li><a tags="string,utility,beginner" href="./string#islowercase">isLowerCase</a></li><li><a tags="string,utility,beginner" href="./string#isuppercase">isUpperCase</a></li><li><a tags="string,array,function,utility,beginner" href="./string#mapstring">mapString</a></li><li><a tags="string,utility,regexp,intermediate" href="./string#mask">mask</a></li><li><a tags="string,beginner" href="./string#pad">pad</a></li><li><a tags="string,intermediate" href="./string#palindrome">palindrome</a></li><li><a tags="string,intermediate" href="./string#pluralize">pluralize</a></li><li><a tags="string,regexp,intermediate" href="./string#removenonascii">removeNonASCII</a></li><li><a tags="string,array,beginner" href="./string#reversestring">reverseString</a></li><li><a tags="string,beginner" href="./string#sortcharactersinstring">sortCharactersInString</a></li><li><a tags="string,beginner" href="./string#splitlines">splitLines</a></li><li><a tags="string,recursion,advanced" href="./string#stringpermutations">stringPermutations</a></li><li><a tags="string,utility,regexp,beginner" href="./string#striphtmltags">stripHTMLTags</a></li><li><a tags="string,regexp,intermediate" href="./string#tocamelcase">toCamelCase</a></li><li><a tags="string,regexp,intermediate" href="./string#tokebabcase">toKebabCase</a></li><li><a tags="string,regexp,intermediate" href="./string#tosnakecase">toSnakeCase</a></li><li><a tags="string,beginner" href="./string#truncatestring">truncateString</a></li><li><a tags="string,browser,beginner" href="./string#unescapehtml">unescapeHTML</a></li><li><a tags="string,utility,regexp,advanced" href="./string#urljoin">URLJoin</a></li><li><a tags="string,regexp,intermediate" href="./string#words">words</a></li></ul><h4 class="collapse">Type</h4><ul><li><a tags="type,beginner" href="./type#gettype">getType</a></li><li><a tags="type,array,regexp,beginner" href="./type#is">is</a></li><li><a tags="type,array,intermediate" href="./type#isarraylike">isArrayLike</a></li><li><a tags="type,beginner" href="./type#isboolean">isBoolean</a></li><li><a tags="type,array,object,string,beginner" href="./type#isempty">isEmpty</a></li><li><a tags="type,function,beginner" href="./type#isfunction">isFunction</a></li><li><a tags="type,beginner" href="./type#isnil">isNil</a></li><li><a tags="type,beginner" href="./type#isnull">isNull</a></li><li><a tags="type,math,beginner" href="./type#isnumber">isNumber</a></li><li><a tags="type,object,beginner" href="./type#isobject">isObject</a></li><li><a tags="type,object,beginner" href="./type#isobjectlike">isObjectLike</a></li><li><a tags="type,object,intermediate" href="./type#isplainobject">isPlainObject</a></li><li><a tags="type,function,array,string,intermediate" href="./type#isprimitive">isPrimitive</a></li><li><a tags="type,function,promise,intermediate" href="./type#ispromiselike">isPromiseLike</a></li><li><a tags="type,string,beginner" href="./type#isstring">isString</a></li><li><a tags="type,beginner" href="./type#issymbol">isSymbol</a></li><li><a tags="type,beginner" href="./type#isundefined">isUndefined</a></li><li><a tags="type,json,intermediate" href="./type#isvalidjson">isValidJSON</a></li></ul><h4 class="collapse">Utility</h4><ul><li><a tags="utility,array,type,beginner" href="./utility#castarray">castArray</a></li><li><a tags="utility,regexp,intermediate" href="./utility#cloneregexp">cloneRegExp</a></li><li><a tags="utility,beginner" href="./utility#coalesce">coalesce</a></li><li><a tags="utility,intermediate" href="./utility#coalescefactory">coalesceFactory</a></li><li><a tags="utility,string,intermediate" href="./utility#extendhex">extendHex</a></li><li><a tags="utility,browser,string,url,intermediate" href="./utility#geturlparameters">getURLParameters</a></li><li><a tags="utility,string,math,advanced" href="./utility#hextorgb">hexToRGB</a></li><li><a tags="utility,url,browser,intermediate" href="./utility#httpget">httpGet</a></li><li><a tags="utility,url,browser,intermediate" href="./utility#httppost">httpPost</a></li><li><a tags="utility,browser,intermediate" href="./utility#isbrowser">isBrowser</a></li><li><a tags="utility,function" href="./utility#mostperformant">mostPerformant</a></li><li><a tags="utility,function,beginner" href="./utility#ntharg">nthArg</a></li><li><a tags="utility,string,intermediate" href="./utility#parsecookie">parseCookie</a></li><li><a tags="utility,string,math,advanced" href="./utility#prettybytes">prettyBytes</a></li><li><a tags="utility,random,beginner" href="./utility#randomhexcolorcode">randomHexColorCode</a></li><li><a tags="utility,intermediate" href="./utility#rgbtohex">RGBToHex</a></li><li><a tags="utility,string,intermediate" href="./utility#serializecookie">serializeCookie</a></li><li><a tags="utility,beginner" href="./utility#timetaken">timeTaken</a></li><li><a tags="utility,intermediate" href="./utility#tocurrency">toCurrency</a></li><li><a tags="utility,math,beginner" href="./utility#todecimalmark">toDecimalMark</a></li><li><a tags="utility,math,intermediate" href="./utility#toordinalsuffix">toOrdinalSuffix</a></li><li><a tags="utility,math,intermediate" href="./utility#validatenumber">validateNumber</a></li><li><a tags="utility,regexp,intermediate" href="./utility#yesno">yesNo</a></li></ul><h4 class="static-link"><a href="./archive">Archive</a></h4><h4 class="static-link"><a href="./glossary">Glossary</a></h4><h4 class="static-link"><a href="./contributing">Contributing</a></h4><h4 class="static-link"><a href="./about">About</a></h4><div><button class="social fb"></button><button class="social instagram"></button><button class="social twitter"></button></div></nav><main class="col-centered"><span id="top"><br/><br/></span><h2 class="category-name">Array</h2><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="all">all</h4><p>Returns <code>true</code> if the provided predicate function returns <code>true</code> for all elements in a collection, <code>false</code> otherwise.</p><p>Use <code>Array.prototype.every()</code> to test if all elements in the collection return <code>true</code> based on <code>fn</code>. Omit the second argument, <code>fn</code>, to use <code>Boolean</code> as a default.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">all</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn <span class="token operator">=</span> Boolean<span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">every</span><span class="token punctuation">(</span>fn<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">all</span><span class="token punctuation">([</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> x <span class="token operator">=></span> x <span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">);</span> <span class="token comment">// true</span>
<span class="token function">all</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// true</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="allequal">allEqual</h4><p>Check if all elements in an array are equal.</p><p>Use <code>Array.prototype.every()</code> to check if all the elements of the array are the same as the first one.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">allEqual</span> <span class="token operator">=</span> arr <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">every</span><span class="token punctuation">(</span>val <span class="token operator">=></span> val <span class="token operator">===</span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">allEqual</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]);</span> <span class="token comment">// false</span>
<span class="token function">allEqual</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]);</span> <span class="token comment">// true</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="any">any</h4><p>Returns <code>true</code> if the provided predicate function returns <code>true</code> for at least one element in a collection, <code>false</code> otherwise.</p><p>Use <code>Array.prototype.some()</code> to test if any elements in the collection return <code>true</code> based on <code>fn</code>. Omit the second argument, <code>fn</code>, to use <code>Boolean</code> as a default.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">any</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn <span class="token operator">=</span> Boolean<span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span>fn<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">any</span><span class="token punctuation">([</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">],</span> x <span class="token operator">=></span> x <span class="token operator">>=</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// true</span>
<span class="token function">any</span><span class="token punctuation">([</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]);</span> <span class="token comment">// true</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="arraytocsv">arrayToCSV</h4><p>Converts a 2D array to a comma-separated values (CSV) string.</p><p>Use <code>Array.prototype.map()</code> and <code>Array.prototype.join(delimiter)</code> to combine individual 1D arrays (rows) into strings. Use <code>Array.prototype.join('\n')</code> to combine all rows into a CSV string, separating each row with a newline. Omit the second argument, <code>delimiter</code>, to use a default delimiter of <code>,</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> arrayToCSV <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> delimiter <span class="token operator">=</span> <span class="token string">','</span><span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>v <span class="token operator">=></span> v<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>x <span class="token operator">=></span> <span class="token template-string"><span class="token string">`"</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>x<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">"`</span></span><span class="token punctuation">).</span><span class="token function">join</span><span class="token punctuation">(</span>delimiter<span class="token punctuation">)).</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'\n'</span><span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">arrayToCSV</span><span class="token punctuation">([[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">], [</span><span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">]]);</span> <span class="token comment">// '"a","b"\n"c","d"'</span>
<span class="token function">arrayToCSV</span><span class="token punctuation">([[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">], [</span><span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">]],</span> <span class="token string">';'</span><span class="token punctuation">);</span> <span class="token comment">// '"a";"b"\n"c";"d"'</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="bifurcate">bifurcate</h4><p>Splits values into two groups. If an element in <code>filter</code> is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.</p><p>Use <code>Array.prototype.reduce()</code> and <code>Array.prototype.push()</code> to add elements to groups, based on <code>filter</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">bifurcate</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> filter<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>acc<span class="token punctuation">[</span>filter<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">?</span> <span class="token number">0</span> <span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">].</span><span class="token function">push</span><span class="token punctuation">(</span>val<span class="token punctuation">),</span> acc<span class="token punctuation">), [[], []]);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">bifurcate</span><span class="token punctuation">([</span><span class="token string">'beep'</span><span class="token punctuation">,</span> <span class="token string">'boop'</span><span class="token punctuation">,</span> <span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token string">'bar'</span><span class="token punctuation">], [</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">]);</span> <span class="token comment">// [ ['beep', 'boop', 'bar'], ['foo'] ]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="bifurcateby">bifurcateBy</h4><p>Splits values into two groups according to a predicate function, which specifies which group an element in the input collection belongs to. If the predicate function returns a truthy value, the collection element belongs to the first group; otherwise, it belongs to the second group.</p><p>Use <code>Array.prototype.reduce()</code> and <code>Array.prototype.push()</code> to add elements to groups, based on the value returned by <code>fn</code> for each element.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">bifurcateBy</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>acc<span class="token punctuation">[</span><span class="token function">fn</span><span class="token punctuation">(</span>val<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token number">0</span> <span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">].</span><span class="token function">push</span><span class="token punctuation">(</span>val<span class="token punctuation">),</span> acc<span class="token punctuation">), [[], []]);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">bifurcateBy</span><span class="token punctuation">([</span><span class="token string">'beep'</span><span class="token punctuation">,</span> <span class="token string">'boop'</span><span class="token punctuation">,</span> <span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token string">'bar'</span><span class="token punctuation">],</span> x <span class="token operator">=></span> x<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token string">'b'</span><span class="token punctuation">);</span> <span class="token comment">// [ ['beep', 'boop', 'bar'], ['foo'] ]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="chunk">chunk</h4><p>Chunks an array into smaller arrays of a specified size.</p><p>Use <code>Array.from()</code> to create a new array, that fits the number of chunks that will be produced. Use <code>Array.prototype.slice()</code> to map each element of the new array to a chunk the length of <code>size</code>. If the original array can't be split evenly, the final chunk will contain the remaining elements.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">chunk</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> size<span class="token punctuation">)</span> <span class="token operator">=></span>
  Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">({</span> length<span class="token punctuation">:</span> Math<span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span>length <span class="token operator">/</span> size<span class="token punctuation">) }, (</span>v<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span>
    arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>i <span class="token operator">*</span> size<span class="token punctuation">,</span> i <span class="token operator">*</span> size <span class="token operator">+</span> size<span class="token punctuation">)
  );</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">chunk</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">],</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [[1,2],[3,4],[5]]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="compact">compact</h4><p>Removes falsey values from an array.</p><p>Use <code>Array.prototype.filter()</code> to filter out falsey values (<code>false</code>, <code>null</code>, <code>0</code>, <code>&quot;&quot;</code>, <code>undefined</code>, and <code>NaN</code>).</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">compact</span> <span class="token operator">=</span> arr <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>Boolean<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">compact</span><span class="token punctuation">([</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'e'</span> <span class="token operator">*</span> <span class="token number">23</span><span class="token punctuation">,</span> <span class="token number">NaN</span><span class="token punctuation">,</span> <span class="token string">'s'</span><span class="token punctuation">,</span> <span class="token number">34</span><span class="token punctuation">]);</span> <span class="token comment">// [ 1, 2, 3, 'a', 's', 34 ]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="countby">countBy</h4><p>Groups the elements of an array based on the given function and returns the count of elements in each group.</p><p>Use <code>Array.prototype.map()</code> to map the values of an array to a function or property name. Use <code>Array.prototype.reduce()</code> to create an object, where the keys are produced from the mapped results.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">countBy</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> fn <span class="token operator">===</span> <span class="token string">'function'</span> <span class="token operator">?</span> fn <span class="token punctuation">:</span> val <span class="token operator">=></span> val<span class="token punctuation">[</span>fn<span class="token punctuation">]).</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
    acc<span class="token punctuation">[</span>val<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>acc<span class="token punctuation">[</span>val<span class="token punctuation">]</span> <span class="token operator">||</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> acc<span class="token punctuation">;
  }, {});</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">countBy</span><span class="token punctuation">([</span><span class="token number">6.1</span><span class="token punctuation">,</span> <span class="token number">4.2</span><span class="token punctuation">,</span> <span class="token number">6.3</span><span class="token punctuation">],</span> Math<span class="token punctuation">.</span>floor<span class="token punctuation">);</span> <span class="token comment">// {4: 1, 6: 2}</span>
<span class="token function">countBy</span><span class="token punctuation">([</span><span class="token string">'one'</span><span class="token punctuation">,</span> <span class="token string">'two'</span><span class="token punctuation">,</span> <span class="token string">'three'</span><span class="token punctuation">],</span> <span class="token string">'length'</span><span class="token punctuation">);</span> <span class="token comment">// {3: 2, 5: 1}</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="countoccurrences">countOccurrences</h4><p>Counts the occurrences of a value in an array.</p><p>Use <code>Array.prototype.reduce()</code> to increment a counter each time you encounter the specific value inside the array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">countOccurrences</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>a<span class="token punctuation">,</span> v<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>v <span class="token operator">===</span> val <span class="token operator">?</span> a <span class="token operator">+</span> <span class="token number">1</span> <span class="token punctuation">:</span> a<span class="token punctuation">),</span> <span class="token number">0</span><span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">countOccurrences</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">1</span><span class="token punctuation">);</span> <span class="token comment">// 3</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="deepflatten">deepFlatten</h4><p>Deep flattens an array.</p><p>Use recursion. Use <code>Array.prototype.concat()</code> with an empty array (<code>[]</code>) and the spread operator (<code>...</code>) to flatten an array. Recursively flatten each element that is an array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">deepFlatten</span> <span class="token operator">=</span> arr <span class="token operator">=></span> <span class="token punctuation">[].</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token operator">...</span>arr<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>v <span class="token operator">=></span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token function">deepFlatten</span><span class="token punctuation">(</span>v<span class="token punctuation">) :</span> v<span class="token punctuation">)));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">deepFlatten</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">, [</span><span class="token number">2</span><span class="token punctuation">], [[</span><span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">4</span><span class="token punctuation">],</span> <span class="token number">5</span><span class="token punctuation">]);</span> <span class="token comment">// [1,2,3,4,5]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="difference">difference</h4><p>Returns the difference between two arrays.</p><p>Create a <code>Set</code> from <code>b</code>, then use <code>Array.prototype.filter()</code> on <code>a</code> to only keep values not contained in <code>b</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">difference</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> s <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span>b<span class="token punctuation">);</span>
  <span class="token keyword">return</span> a<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">=> !</span>s<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span>x<span class="token punctuation">));
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">difference</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">], [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]);</span> <span class="token comment">// [3]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="differenceby">differenceBy</h4><p>Returns the difference between two arrays, after applying the provided function to each array element of both.</p><p>Create a <code>Set</code> by applying <code>fn</code> to each element in <code>b</code>, then use <code>Array.prototype.filter()</code> in combination with <code>fn</code> on <code>a</code> to only keep values not contained in the previously created set.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">differenceBy</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> s <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span>b<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>v <span class="token operator">=></span> <span class="token function">fn</span><span class="token punctuation">(</span>v<span class="token punctuation">)));</span>
  <span class="token keyword">return</span> a<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">=> !</span>s<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token function">fn</span><span class="token punctuation">(</span>x<span class="token punctuation">)));
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">differenceBy</span><span class="token punctuation">([</span><span class="token number">2.1</span><span class="token punctuation">,</span> <span class="token number">1.2</span><span class="token punctuation">], [</span><span class="token number">2.3</span><span class="token punctuation">,</span> <span class="token number">3.4</span><span class="token punctuation">],</span> Math<span class="token punctuation">.</span>floor<span class="token punctuation">);</span> <span class="token comment">// [1.2]</span>
<span class="token function">differenceBy</span><span class="token punctuation">([{</span> x<span class="token punctuation">:</span> <span class="token number">2</span> <span class="token punctuation">}, {</span> x<span class="token punctuation">:</span> <span class="token number">1</span> <span class="token punctuation">}], [{</span> x<span class="token punctuation">:</span> <span class="token number">1</span> <span class="token punctuation">}],</span> v <span class="token operator">=></span> v<span class="token punctuation">.</span>x<span class="token punctuation">);</span> <span class="token comment">// [ { x: 2 } ]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="differencewith">differenceWith</h4><p>Filters out all values from an array for which the comparator function does not return <code>true</code>.</p><p>Use <code>Array.prototype.filter()</code> and <code>Array.prototype.findIndex()</code> to find the appropriate values.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">differenceWith</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> val<span class="token punctuation">,</span> comp<span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>a <span class="token operator">=></span> val<span class="token punctuation">.</span><span class="token function">findIndex</span><span class="token punctuation">(</span>b <span class="token operator">=></span> <span class="token function">comp</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">))</span> <span class="token operator">=== -</span><span class="token number">1</span><span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">differenceWith</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1.2</span><span class="token punctuation">,</span> <span class="token number">1.5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">], [</span><span class="token number">1.9</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">], (</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token operator">===</span> Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span>b<span class="token punctuation">));</span> <span class="token comment">// [1, 1.2]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="drop">drop</h4><p>Returns a new array with <code>n</code> elements removed from the left.</p><p>Use <code>Array.prototype.slice()</code> to slice the remove the specified number of elements from the left.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">drop</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> n <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>n<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">drop</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// [2,3]</span>
<span class="token function">drop</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [3]</span>
<span class="token function">drop</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">42</span><span class="token punctuation">);</span> <span class="token comment">// []</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="dropright">dropRight</h4><p>Returns a new array with <code>n</code> elements removed from the right.</p><p>Use <code>Array.prototype.slice()</code> to slice the remove the specified number of elements from the right.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">dropRight</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> n <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span>n<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">dropRight</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// [1,2]</span>
<span class="token function">dropRight</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [1]</span>
<span class="token function">dropRight</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">42</span><span class="token punctuation">);</span> <span class="token comment">// []</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="droprightwhile">dropRightWhile</h4><p>Removes elements from the end of an array until the passed function returns <code>true</code>. Returns the remaining elements in the array.</p><p>Loop through the array, using <code>Array.prototype.slice()</code> to drop the last element of the array until the returned value from the function is <code>true</code>. Returns the remaining elements.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">dropRightWhile</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> func<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">while</span> <span class="token punctuation">(</span>arr<span class="token punctuation">.</span>length <span class="token operator">></span> <span class="token number">0</span> <span class="token operator">&amp;&amp; !</span><span class="token function">func</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]))</span> arr <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">);</span>
  <span class="token keyword">return</span> arr<span class="token punctuation">;
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">dropRightWhile</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">],</span> n <span class="token operator">=></span> n <span class="token operator">&lt;</span> <span class="token number">3</span><span class="token punctuation">);</span> <span class="token comment">// [1, 2]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="dropwhile">dropWhile</h4><p>Removes elements in an array until the passed function returns <code>true</code>. Returns the remaining elements in the array.</p><p>Loop through the array, using <code>Array.prototype.slice()</code> to drop the first element of the array until the returned value from the function is <code>true</code>. Returns the remaining elements.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">dropWhile</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> func<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">while</span> <span class="token punctuation">(</span>arr<span class="token punctuation">.</span>length <span class="token operator">></span> <span class="token number">0</span> <span class="token operator">&amp;&amp; !</span><span class="token function">func</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]))</span> arr <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">);</span>
  <span class="token keyword">return</span> arr<span class="token punctuation">;
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">dropWhile</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">],</span> n <span class="token operator">=></span> n <span class="token operator">>=</span> <span class="token number">3</span><span class="token punctuation">);</span> <span class="token comment">// [3,4]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="everynth">everyNth</h4><p>Returns every nth element in an array.</p><p>Use <code>Array.prototype.filter()</code> to create a new array that contains every nth element of a given array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">everyNth</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> nth<span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">((</span>e<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> i <span class="token operator">%</span> nth <span class="token operator">===</span> nth <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">everyNth</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">],</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [ 2, 4, 6 ]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="filternonunique">filterNonUnique</h4><p>Filters out the non-unique values in an array.</p><p>Use <code>Array.prototype.filter()</code> for an array containing only the unique values.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">filterNonUnique</span> <span class="token operator">=</span> arr <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>i <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token operator">===</span> arr<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span>i<span class="token punctuation">));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">filterNonUnique</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]);</span> <span class="token comment">// [1, 3, 5]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="filternonuniqueby">filterNonUniqueBy</h4><p>Filters out the non-unique values in an array, based on a provided comparator function.</p><p>Use <code>Array.prototype.filter()</code> and <code>Array.prototype.every()</code> for an array containing only the unique values, based on the comparator function, <code>fn</code>. The comparator function takes four arguments: the values of the two elements being compared and their indexes.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">filterNonUniqueBy</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">((</span>v<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">every</span><span class="token punctuation">((</span>x<span class="token punctuation">,</span> j<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>i <span class="token operator">===</span> j<span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token function">fn</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span> x<span class="token punctuation">,</span> i<span class="token punctuation">,</span> j<span class="token punctuation">)));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">filterNonUniqueBy</span><span class="token punctuation">(
  [
    {</span> id<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'a'</span> <span class="token punctuation">},
    {</span> id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'b'</span> <span class="token punctuation">},
    {</span> id<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'c'</span> <span class="token punctuation">},
    {</span> id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'d'</span> <span class="token punctuation">},
    {</span> id<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'e'</span> <span class="token punctuation">}
  ],
  (</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> a<span class="token punctuation">.</span>id <span class="token operator">==</span> b<span class="token punctuation">.</span>id
<span class="token punctuation">);</span> <span class="token comment">// [ { id: 2, value: 'c' } ]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="findlast">findLast</h4><p>Returns the last element for which the provided function returns a truthy value.</p><p>Use <code>Array.prototype.filter()</code> to remove elements for which <code>fn</code> returns falsey values, <code>Array.prototype.pop()</code> to get the last one.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">findLast</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>fn<span class="token punctuation">).</span><span class="token function">pop</span><span class="token punctuation">();</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">findLast</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">],</span> n <span class="token operator">=></span> n <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">===</span> <span class="token number">1</span><span class="token punctuation">);</span> <span class="token comment">// 3</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="findlastindex">findLastIndex</h4><p>Returns the index of the last element for which the provided function returns a truthy value.</p><p>Use <code>Array.prototype.map()</code> to map each element to an array with its index and value. Use <code>Array.prototype.filter()</code> to remove elements for which <code>fn</code> returns falsey values, <code>Array.prototype.pop()</code> to get the last one.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">findLastIndex</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr
    <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">((</span>val<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">[</span>i<span class="token punctuation">,</span> val<span class="token punctuation">])
    .</span><span class="token function">filter</span><span class="token punctuation">(([</span>i<span class="token punctuation">,</span> val<span class="token punctuation">])</span> <span class="token operator">=></span> <span class="token function">fn</span><span class="token punctuation">(</span>val<span class="token punctuation">,</span> i<span class="token punctuation">,</span> arr<span class="token punctuation">))
    .</span><span class="token function">pop</span><span class="token punctuation">()[</span><span class="token number">0</span><span class="token punctuation">];</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">findLastIndex</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">],</span> n <span class="token operator">=></span> n <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">===</span> <span class="token number">1</span><span class="token punctuation">);</span> <span class="token comment">// 2 (index of the value 3)</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="flatten">flatten</h4><p>Flattens an array up to the specified depth.</p><p>Use recursion, decrementing <code>depth</code> by 1 for each level of depth. Use <code>Array.prototype.reduce()</code> and <code>Array.prototype.concat()</code> to merge elements or arrays. Base case, for <code>depth</code> equal to <code>1</code> stops recursion. Omit the second argument, <code>depth</code> to flatten only to a depth of <code>1</code> (single flatten).</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">flatten</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> depth <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>a<span class="token punctuation">,</span> v<span class="token punctuation">)</span> <span class="token operator">=></span> a<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>depth <span class="token operator">></span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token function">flatten</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span> depth <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">) :</span> v<span class="token punctuation">), []);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">flatten</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">, [</span><span class="token number">2</span><span class="token punctuation">],</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]);</span> <span class="token comment">// [1, 2, 3, 4]</span>
<span class="token function">flatten</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">, [</span><span class="token number">2</span><span class="token punctuation">, [</span><span class="token number">3</span><span class="token punctuation">, [</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">],</span> <span class="token number">6</span><span class="token punctuation">],</span> <span class="token number">7</span><span class="token punctuation">],</span> <span class="token number">8</span><span class="token punctuation">],</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [1, 2, 3, [4, 5], 6, 7, 8]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="foreachright">forEachRight</h4><p>Executes a provided function once for each array element, starting from the array's last element.</p><p>Use <code>Array.prototype.slice(0)</code> to clone the given array, <code>Array.prototype.reverse()</code> to reverse it and <code>Array.prototype.forEach()</code> to iterate over the reversed array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">forEachRight</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> callback<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr
    <span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)
    .</span><span class="token function">reverse</span><span class="token punctuation">()
    .</span><span class="token function">forEach</span><span class="token punctuation">(</span>callback<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">forEachRight</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">],</span> val <span class="token operator">=></span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>val<span class="token punctuation">));</span> <span class="token comment">// '4', '3', '2', '1'</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="groupby">groupBy</h4><p>Groups the elements of an array based on the given function.</p><p>Use <code>Array.prototype.map()</code> to map the values of an array to a function or property name. Use <code>Array.prototype.reduce()</code> to create an object, where the keys are produced from the mapped results.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">groupBy</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> fn <span class="token operator">===</span> <span class="token string">'function'</span> <span class="token operator">?</span> fn <span class="token punctuation">:</span> val <span class="token operator">=></span> val<span class="token punctuation">[</span>fn<span class="token punctuation">]).</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
    acc<span class="token punctuation">[</span>val<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>acc<span class="token punctuation">[</span>val<span class="token punctuation">]</span> <span class="token operator">||</span> <span class="token punctuation">[]).</span><span class="token function">concat</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]);</span>
    <span class="token keyword">return</span> acc<span class="token punctuation">;
  }, {});</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">groupBy</span><span class="token punctuation">([</span><span class="token number">6.1</span><span class="token punctuation">,</span> <span class="token number">4.2</span><span class="token punctuation">,</span> <span class="token number">6.3</span><span class="token punctuation">],</span> Math<span class="token punctuation">.</span>floor<span class="token punctuation">);</span> <span class="token comment">// {4: [4.2], 6: [6.1, 6.3]}</span>
<span class="token function">groupBy</span><span class="token punctuation">([</span><span class="token string">'one'</span><span class="token punctuation">,</span> <span class="token string">'two'</span><span class="token punctuation">,</span> <span class="token string">'three'</span><span class="token punctuation">],</span> <span class="token string">'length'</span><span class="token punctuation">);</span> <span class="token comment">// {3: ['one', 'two'], 5: ['three']}</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="head">head</h4><p>Returns the head of a list.</p><p>Use <code>arr[0]</code> to return the first element of the passed array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">head</span> <span class="token operator">=</span> arr <span class="token operator">=></span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">];</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">head</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// 1</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="indexofall">indexOfAll</h4><p>Returns all indices of <code>val</code> in an array. If <code>val</code> never occurs, returns <code>[]</code>.</p><p>Use <code>Array.prototype.reduce()</code> to loop over elements and store indices for matching elements. Return the array of indices.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">indexOfAll</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> el<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>el <span class="token operator">===</span> val <span class="token operator">?</span> <span class="token punctuation">[</span><span class="token operator">...</span>acc<span class="token punctuation">,</span> i<span class="token punctuation">] :</span> acc<span class="token punctuation">), []);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">indexOfAll</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">1</span><span class="token punctuation">);</span> <span class="token comment">// [0,3]</span>
<span class="token function">indexOfAll</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">4</span><span class="token punctuation">);</span> <span class="token comment">// []</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="initial">initial</h4><p>Returns all the elements of an array except the last one.</p><p>Use <code>arr.slice(0,-1)</code> to return all but the last element of the array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">initial</span> <span class="token operator">=</span> arr <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">initial</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// [1,2]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="initialize2darray">initialize2DArray</h4><p>Initializes a 2D array of given width and height and value.</p><p>Use <code>Array.prototype.map()</code> to generate h rows where each is a new array of size w initialize with value. If the value is not provided, default to <code>null</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">initialize2DArray</span> <span class="token operator">=</span> <span class="token punctuation">(</span>w<span class="token punctuation">,</span> h<span class="token punctuation">,</span> val <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token operator">=></span>
  Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">({</span> length<span class="token punctuation">:</span> h <span class="token punctuation">}).</span><span class="token function">map</span><span class="token punctuation">(()</span> <span class="token operator">=></span> Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">({</span> length<span class="token punctuation">:</span> w <span class="token punctuation">}).</span><span class="token function">fill</span><span class="token punctuation">(</span>val<span class="token punctuation">));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">initialize2DArray</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">);</span> <span class="token comment">// [[0,0], [0,0]]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="initializearraywithrange">initializeArrayWithRange</h4><p>Initializes an array containing the numbers in the specified range where <code>start</code> and <code>end</code> are inclusive with their common difference <code>step</code>.</p><p>Use <code>Array.from()</code> to create an array of the desired length, <code>(end - start + 1)/step</code>, and a map function to fill it with the desired values in the given range. You can omit <code>start</code> to use a default value of <code>0</code>. You can omit <code>step</code> to use a default value of <code>1</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">initializeArrayWithRange</span> <span class="token operator">=</span> <span class="token punctuation">(</span>end<span class="token punctuation">,</span> start <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> step <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=></span>
  Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">({</span> length<span class="token punctuation">:</span> Math<span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">((</span>end <span class="token operator">-</span> start <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> step<span class="token punctuation">) }, (</span>v<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> i <span class="token operator">*</span> step <span class="token operator">+</span> start<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">initializeArrayWithRange</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">);</span> <span class="token comment">// [0,1,2,3,4,5]</span>
<span class="token function">initializeArrayWithRange</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">);</span> <span class="token comment">// [3,4,5,6,7]</span>
<span class="token function">initializeArrayWithRange</span><span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [0,2,4,6,8]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="initializearraywithrangeright">initializeArrayWithRangeRight</h4><p>Initializes an array containing the numbers in the specified range (in reverse) where <code>start</code> and <code>end</code> are inclusive with their common difference <code>step</code>.</p><p>Use <code>Array.from(Math.ceil((end+1-start)/step))</code> to create an array of the desired length(the amounts of elements is equal to <code>(end-start)/step</code> or <code>(end+1-start)/step</code> for inclusive end), <code>Array.prototype.map()</code> to fill with the desired values in a range. You can omit <code>start</code> to use a default value of <code>0</code>. You can omit <code>step</code> to use a default value of <code>1</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">initializeArrayWithRangeRight</span> <span class="token operator">=</span> <span class="token punctuation">(</span>end<span class="token punctuation">,</span> start <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> step <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=></span>
  Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">({</span> length<span class="token punctuation">:</span> Math<span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">((</span>end <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">-</span> start<span class="token punctuation">)</span> <span class="token operator">/</span> step<span class="token punctuation">) }).</span><span class="token function">map</span><span class="token punctuation">(
    (</span>v<span class="token punctuation">,</span> i<span class="token punctuation">,</span> arr<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>arr<span class="token punctuation">.</span>length <span class="token operator">-</span> i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> step <span class="token operator">+</span> start
  <span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">initializeArrayWithRangeRight</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">);</span> <span class="token comment">// [5,4,3,2,1,0]</span>
<span class="token function">initializeArrayWithRangeRight</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">);</span> <span class="token comment">// [7,6,5,4,3]</span>
<span class="token function">initializeArrayWithRangeRight</span><span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [8,6,4,2,0]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="initializearraywithvalues">initializeArrayWithValues</h4><p>Initializes and fills an array with the specified values.</p><p>Use <code>Array(n)</code> to create an array of the desired length, <code>fill(v)</code> to fill it with the desired values. You can omit <code>val</code> to use a default value of <code>0</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">initializeArrayWithValues</span> <span class="token operator">=</span> <span class="token punctuation">(</span>n<span class="token punctuation">,</span> val <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">Array</span><span class="token punctuation">(</span>n<span class="token punctuation">).</span><span class="token function">fill</span><span class="token punctuation">(</span>val<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">initializeArrayWithValues</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [2, 2, 2, 2, 2]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="initializendarray">initializeNDArray</h4><p>Create a n-dimensional array with given value.</p><p>Use recursion. Use <code>Array.prototype.map()</code> to generate rows where each is a new array initialized using <code>initializeNDArray</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">initializeNDArray</span> <span class="token operator">=</span> <span class="token punctuation">(</span>val<span class="token punctuation">,</span> <span class="token operator">...</span>args<span class="token punctuation">)</span> <span class="token operator">=></span>
  args<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">0</span>
    <span class="token operator">?</span> val
    <span class="token punctuation">:</span> Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">({</span> length<span class="token punctuation">:</span> args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">] }).</span><span class="token function">map</span><span class="token punctuation">(()</span> <span class="token operator">=></span> <span class="token function">initializeNDArray</span><span class="token punctuation">(</span>val<span class="token punctuation">,</span> <span class="token operator">...</span>args<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">initializeNDArray</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">);</span> <span class="token comment">// [1,1,1]</span>
<span class="token function">initializeNDArray</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [[[5,5],[5,5]],[[5,5],[5,5]]]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="intersection">intersection</h4><p>Returns a list of elements that exist in both arrays.</p><p>Create a <code>Set</code> from <code>b</code>, then use <code>Array.prototype.filter()</code> on <code>a</code> to only keep values contained in <code>b</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">intersection</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> s <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span>b<span class="token punctuation">);</span>
  <span class="token keyword">return</span> a<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">=></span> s<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span>x<span class="token punctuation">));
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">intersection</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">], [</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]);</span> <span class="token comment">// [2, 3]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="intersectionby">intersectionBy</h4><p>Returns a list of elements that exist in both arrays, after applying the provided function to each array element of both.</p><p>Create a <code>Set</code> by applying <code>fn</code> to all elements in <code>b</code>, then use <code>Array.prototype.filter()</code> on <code>a</code> to only keep elements, which produce values contained in <code>b</code> when <code>fn</code> is applied to them.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">intersectionBy</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> s <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span>b<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>fn<span class="token punctuation">));</span>
  <span class="token keyword">return</span> a<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">=></span> s<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token function">fn</span><span class="token punctuation">(</span>x<span class="token punctuation">)));
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">intersectionBy</span><span class="token punctuation">([</span><span class="token number">2.1</span><span class="token punctuation">,</span> <span class="token number">1.2</span><span class="token punctuation">], [</span><span class="token number">2.3</span><span class="token punctuation">,</span> <span class="token number">3.4</span><span class="token punctuation">],</span> Math<span class="token punctuation">.</span>floor<span class="token punctuation">);</span> <span class="token comment">// [2.1]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="intersectionwith">intersectionWith</h4><p>Returns a list of elements that exist in both arrays, using a provided comparator function.</p><p>Use <code>Array.prototype.filter()</code> and <code>Array.prototype.findIndex()</code> in combination with the provided comparator to determine intersecting values.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">intersectionWith</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> comp<span class="token punctuation">)</span> <span class="token operator">=></span> a<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">=></span> b<span class="token punctuation">.</span><span class="token function">findIndex</span><span class="token punctuation">(</span>y <span class="token operator">=></span> <span class="token function">comp</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">))</span> <span class="token operator">!== -</span><span class="token number">1</span><span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">intersectionWith</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1.2</span><span class="token punctuation">,</span> <span class="token number">1.5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">], [</span><span class="token number">1.9</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">3.9</span><span class="token punctuation">], (</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token operator">===</span> Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span>b<span class="token punctuation">));</span> <span class="token comment">// [1.5, 3, 0]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="issorted">isSorted</h4><p>Returns <code>1</code> if the array is sorted in ascending order, <code>-1</code> if it is sorted in descending order or <code>0</code> if it is not sorted.</p><p>Calculate the ordering <code>direction</code> for the first two elements. Use <code>Object.entries()</code> to loop over array objects and compare them in pairs. Return <code>0</code> if the <code>direction</code> changes or the <code>direction</code> if the last element is reached.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">isSorted</span> <span class="token operator">=</span> arr <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> direction <span class="token operator">= -</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">-</span> arr<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]);</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> <span class="token punctuation">[</span>i<span class="token punctuation">,</span> val<span class="token punctuation">]</span> <span class="token keyword">of</span> arr<span class="token punctuation">.</span><span class="token function">entries</span><span class="token punctuation">()) {</span>
    direction <span class="token operator">= !</span>direction <span class="token operator">? -</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">-</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]) :</span> direction<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">===</span> arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">!</span>direction <span class="token operator">?</span> <span class="token number">0</span> <span class="token punctuation">:</span> direction<span class="token punctuation">;</span>
    <span class="token keyword">else if</span> <span class="token punctuation">((</span>val <span class="token operator">-</span> arr<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">])</span> <span class="token operator">*</span> direction <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;
  }
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">isSorted</span><span class="token punctuation">([</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]);</span> <span class="token comment">// 1</span>
<span class="token function">isSorted</span><span class="token punctuation">([</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]);</span> <span class="token comment">// -1</span>
<span class="token function">isSorted</span><span class="token punctuation">([</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]);</span> <span class="token comment">// 0</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="join">join</h4><p>Joins all elements of an array into a string and returns this string. Uses a separator and an end separator.</p><p>Use <code>Array.prototype.reduce()</code> to combine elements into a string. Omit the second argument, <code>separator</code>, to use a default separator of <code>','</code>. Omit the third argument, <code>end</code>, to use the same value as <code>separator</code> by default.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> join <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> separator <span class="token operator">=</span> <span class="token string">','</span><span class="token punctuation">,</span> end <span class="token operator">=</span> separator<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(
    (</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span>
      i <span class="token operator">===</span> arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">2</span>
        <span class="token operator">?</span> acc <span class="token operator">+</span> val <span class="token operator">+</span> end
        <span class="token punctuation">:</span> i <span class="token operator">===</span> arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span>
          <span class="token operator">?</span> acc <span class="token operator">+</span> val
          <span class="token punctuation">:</span> acc <span class="token operator">+</span> val <span class="token operator">+</span> separator<span class="token punctuation">,</span>
    <span class="token string">''</span>
  <span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">join</span><span class="token punctuation">([</span><span class="token string">'pen'</span><span class="token punctuation">,</span> <span class="token string">'pineapple'</span><span class="token punctuation">,</span> <span class="token string">'apple'</span><span class="token punctuation">,</span> <span class="token string">'pen'</span><span class="token punctuation">],</span> <span class="token string">','</span><span class="token punctuation">,</span> <span class="token string">'&amp;'</span><span class="token punctuation">);</span> <span class="token comment">// "pen,pineapple,apple&amp;pen"</span>
<span class="token function">join</span><span class="token punctuation">([</span><span class="token string">'pen'</span><span class="token punctuation">,</span> <span class="token string">'pineapple'</span><span class="token punctuation">,</span> <span class="token string">'apple'</span><span class="token punctuation">,</span> <span class="token string">'pen'</span><span class="token punctuation">],</span> <span class="token string">','</span><span class="token punctuation">);</span> <span class="token comment">// "pen,pineapple,apple,pen"</span>
<span class="token function">join</span><span class="token punctuation">([</span><span class="token string">'pen'</span><span class="token punctuation">,</span> <span class="token string">'pineapple'</span><span class="token punctuation">,</span> <span class="token string">'apple'</span><span class="token punctuation">,</span> <span class="token string">'pen'</span><span class="token punctuation">]);</span> <span class="token comment">// "pen,pineapple,apple,pen"</span>
</pre></div><div class="card code-card"><div class="corner advanced"></div><div class="section card-content"><h4 id="jsontocsv">JSONtoCSV</h4><p>Converts an array of objects to a comma-separated values (CSV) string that contains only the <code>columns</code> specified.</p><p>Use <code>Array.prototype.join(demiliter)</code> to combine all the names in <code>columns</code> to create the first row. Use <code>Array.prototype.map()</code> and <code>Array.prototype.reduce()</code> to create a row for each object, substituting non-existent values with empty strings and only mapping values in <code>columns</code>. Use <code>Array.prototype.join('\n')</code> to combine all rows into a string. Omit the third argument, <code>delimiter</code>, to use a default delimiter of <code>,</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> JSONtoCSV <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> columns<span class="token punctuation">,</span> delimiter <span class="token operator">=</span> <span class="token string">','</span><span class="token punctuation">)</span> <span class="token operator">=></span>
  <span class="token punctuation">[</span>
    columns<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>delimiter<span class="token punctuation">),</span>
    <span class="token operator">...</span>arr<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>obj <span class="token operator">=></span>
      columns<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(
        (</span>acc<span class="token punctuation">,</span> key<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token template-string"><span class="token string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>acc<span class="token interpolation-punctuation punctuation">}</span></span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token operator">!</span>acc<span class="token punctuation">.</span>length <span class="token operator">?</span> <span class="token string">''</span> <span class="token punctuation">:</span> delimiter<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">"</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token operator">!</span>obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span class="token operator">?</span> <span class="token string">''</span> <span class="token punctuation">:</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">"`</span></span><span class="token punctuation">,</span>
        <span class="token string">''</span>
      <span class="token punctuation">)
    )
  ].</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'\n'</span><span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">JSONtoCSV</span><span class="token punctuation">([{</span> a<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> b<span class="token punctuation">:</span> <span class="token number">2</span> <span class="token punctuation">}, {</span> a<span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">,</span> b<span class="token punctuation">:</span> <span class="token number">4</span><span class="token punctuation">,</span> c<span class="token punctuation">:</span> <span class="token number">5</span> <span class="token punctuation">}, {</span> a<span class="token punctuation">:</span> <span class="token number">6</span> <span class="token punctuation">}, {</span> b<span class="token punctuation">:</span> <span class="token number">7</span> <span class="token punctuation">}], [</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">]);</span> <span class="token comment">// 'a,b\n"1","2"\n"3","4"\n"6",""\n"","7"'</span>
<span class="token function">JSONtoCSV</span><span class="token punctuation">([{</span> a<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> b<span class="token punctuation">:</span> <span class="token number">2</span> <span class="token punctuation">}, {</span> a<span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">,</span> b<span class="token punctuation">:</span> <span class="token number">4</span><span class="token punctuation">,</span> c<span class="token punctuation">:</span> <span class="token number">5</span> <span class="token punctuation">}, {</span> a<span class="token punctuation">:</span> <span class="token number">6</span> <span class="token punctuation">}, {</span> b<span class="token punctuation">:</span> <span class="token number">7</span> <span class="token punctuation">}], [</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">],</span> <span class="token string">';'</span><span class="token punctuation">);</span> <span class="token comment">// 'a;b\n"1";"2"\n"3";"4"\n"6";""\n"";"7"'</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="last">last</h4><p>Returns the last element in an array.</p><p>Use <code>arr.length - 1</code> to compute the index of the last element of the given array and returning it.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">last</span> <span class="token operator">=</span> arr <span class="token operator">=></span> arr<span class="token punctuation">[</span>arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">];</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">last</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// 3</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="longestitem">longestItem</h4><p>Takes any number of iterable objects or objects with a <code>length</code> property and returns the longest one. If multiple objects have the same length, the first one will be returned. Returns <code>undefined</code> if no arguments are provided.</p><p>Use <code>Array.prototype.reduce()</code>, comparing the <code>length</code> of objects to find the longest one.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">longestItem</span> <span class="token operator">=</span> <span class="token punctuation">(</span>val<span class="token punctuation">,</span> <span class="token operator">...</span>vals<span class="token punctuation">)</span> <span class="token operator">=></span>
  <span class="token punctuation">[</span>val<span class="token punctuation">,</span> <span class="token operator">...</span>vals<span class="token punctuation">].</span><span class="token function">reduce</span><span class="token punctuation">((</span>a<span class="token punctuation">,</span> x<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>x<span class="token punctuation">.</span>length <span class="token operator">></span> a<span class="token punctuation">.</span>length <span class="token operator">?</span> x <span class="token punctuation">:</span> a<span class="token punctuation">));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">longestItem</span><span class="token punctuation">(</span><span class="token string">'this'</span><span class="token punctuation">,</span> <span class="token string">'is'</span><span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'testcase'</span><span class="token punctuation">);</span> <span class="token comment">// 'testcase'</span>
<span class="token function">longestItem</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'ab'</span><span class="token punctuation">,</span> <span class="token string">'abc'</span><span class="token punctuation">]);</span> <span class="token comment">// 'abc'</span>
<span class="token function">longestItem</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'ab'</span><span class="token punctuation">,</span> <span class="token string">'abc'</span><span class="token punctuation">],</span> <span class="token string">'abcd'</span><span class="token punctuation">);</span> <span class="token comment">// 'abcd'</span>
<span class="token function">longestItem</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">], [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">], [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]);</span> <span class="token comment">// [1, 2, 3, 4, 5]</span>
<span class="token function">longestItem</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token string">'foobar'</span><span class="token punctuation">);</span> <span class="token comment">// 'foobar'</span>
</pre></div><div class="card code-card"><div class="corner advanced"></div><div class="section card-content"><h4 id="mapobject">mapObject</h4><p>Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value.</p><p>Use an anonymous inner function scope to declare an undefined memory space, using closures to store a return value. Use a new <code>Array</code> to store the array with a map of the function over its data set and a comma operator to return a second step, without needing to move from one context to another (due to closures and order of operations).</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">mapObject</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span>
  <span class="token punctuation">(</span>a <span class="token operator">=></span> <span class="token punctuation">(
    (</span>a <span class="token operator">=</span> <span class="token punctuation">[</span>arr<span class="token punctuation">,</span> arr<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>fn<span class="token punctuation">)]),</span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">].</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">,</span> ind<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">((</span>acc<span class="token punctuation">[</span>val<span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">][</span>ind<span class="token punctuation">]),</span> acc<span class="token punctuation">), {})
  ))();</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token keyword">const</span> <span class="token function-variable function">squareIt</span> <span class="token operator">=</span> arr <span class="token operator">=></span> <span class="token function">mapObject</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> a <span class="token operator">=></span> a <span class="token operator">*</span> a<span class="token punctuation">);</span>
<span class="token function">squareIt</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// { 1: 1, 2: 4, 3: 9 }</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="maxn">maxN</h4><p>Returns the <code>n</code> maximum elements from the provided array. If <code>n</code> is greater than or equal to the provided array's length, then return the original array (sorted in descending order).</p><p>Use <code>Array.prototype.sort()</code> combined with the spread operator (<code>...</code>) to create a shallow clone of the array and sort it in descending order. Use <code>Array.prototype.slice()</code> to get the specified number of elements. Omit the second argument, <code>n</code>, to get a one-element array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">maxN</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> n <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token operator">...</span>arr<span class="token punctuation">].</span><span class="token function">sort</span><span class="token punctuation">((</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> b <span class="token operator">-</span> a<span class="token punctuation">).</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> n<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">maxN</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// [3]</span>
<span class="token function">maxN</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [3,2]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="minn">minN</h4><p>Returns the <code>n</code> minimum elements from the provided array. If <code>n</code> is greater than or equal to the provided array's length, then return the original array (sorted in ascending order).</p><p>Use <code>Array.prototype.sort()</code> combined with the spread operator (<code>...</code>) to create a shallow clone of the array and sort it in ascending order. Use <code>Array.prototype.slice()</code> to get the specified number of elements. Omit the second argument, <code>n</code>, to get a one-element array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">minN</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> n <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token operator">...</span>arr<span class="token punctuation">].</span><span class="token function">sort</span><span class="token punctuation">((</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> a <span class="token operator">-</span> b<span class="token punctuation">).</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> n<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">minN</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// [1]</span>
<span class="token function">minN</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [1,2]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="none">none</h4><p>Returns <code>true</code> if the provided predicate function returns <code>false</code> for all elements in a collection, <code>false</code> otherwise.</p><p>Use <code>Array.prototype.some()</code> to test if any elements in the collection return <code>true</code> based on <code>fn</code>. Omit the second argument, <code>fn</code>, to use <code>Boolean</code> as a default.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">none</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn <span class="token operator">=</span> Boolean<span class="token punctuation">)</span> <span class="token operator">=> !</span>arr<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span>fn<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">none</span><span class="token punctuation">([</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">],</span> x <span class="token operator">=></span> x <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// true</span>
<span class="token function">none</span><span class="token punctuation">([</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]);</span> <span class="token comment">// true</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="nthelement">nthElement</h4><p>Returns the nth element of an array.</p><p>Use <code>Array.prototype.slice()</code> to get an array containing the nth element at the first place. If the index is out of bounds, return <code>undefined</code>. Omit the second argument, <code>n</code>, to get the first element of the array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">nthElement</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> n <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>n <span class="token operator">=== -</span><span class="token number">1</span> <span class="token operator">?</span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>n<span class="token punctuation">) :</span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span> n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">))[</span><span class="token number">0</span><span class="token punctuation">];</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">nthElement</span><span class="token punctuation">([</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">],</span> <span class="token number">1</span><span class="token punctuation">);</span> <span class="token comment">// 'b'</span>
<span class="token function">nthElement</span><span class="token punctuation">([</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">],</span> <span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">);</span> <span class="token comment">// 'a'</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="offset">offset</h4><p>Moves the specified amount of elements to the end of the array.</p><p>Use <code>Array.prototype.slice()</code> twice to get the elements after the specified index and the elements before that. Use the spread operator(<code>...</code>) to combine the two into one array. If <code>offset</code> is negative, the elements will be moved from end to start.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">offset</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> offset<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token operator">...</span>arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>offset<span class="token punctuation">),</span> <span class="token operator">...</span>arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> offset<span class="token punctuation">)];</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">offset</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">],</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [3, 4, 5, 1, 2]</span>
<span class="token function">offset</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">],</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [4, 5, 1, 2, 3]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="partition">partition</h4><p>Groups the elements into two arrays, depending on the provided function's truthiness for each element.</p><p>Use <code>Array.prototype.reduce()</code> to create an array of two arrays. Use <code>Array.prototype.push()</code> to add elements for which <code>fn</code> returns <code>true</code> to the first array and elements for which <code>fn</code> returns <code>false</code> to the second one.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">partition</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(
    (</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">,</span> i<span class="token punctuation">,</span> arr<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
      acc<span class="token punctuation">[</span><span class="token function">fn</span><span class="token punctuation">(</span>val<span class="token punctuation">,</span> i<span class="token punctuation">,</span> arr<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token number">0</span> <span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">].</span><span class="token function">push</span><span class="token punctuation">(</span>val<span class="token punctuation">);</span>
      <span class="token keyword">return</span> acc<span class="token punctuation">;
    },
    [[], []]
  );</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token keyword">const</span> users <span class="token operator">=</span> <span class="token punctuation">[{</span> user<span class="token punctuation">:</span> <span class="token string">'barney'</span><span class="token punctuation">,</span> age<span class="token punctuation">:</span> <span class="token number">36</span><span class="token punctuation">,</span> active<span class="token punctuation">:</span> <span class="token boolean">false</span> <span class="token punctuation">}, {</span> user<span class="token punctuation">:</span> <span class="token string">'fred'</span><span class="token punctuation">,</span> age<span class="token punctuation">:</span> <span class="token number">40</span><span class="token punctuation">,</span> active<span class="token punctuation">:</span> <span class="token boolean">true</span> <span class="token punctuation">}];</span>
<span class="token function">partition</span><span class="token punctuation">(</span>users<span class="token punctuation">,</span> o <span class="token operator">=></span> o<span class="token punctuation">.</span>active<span class="token punctuation">);</span> <span class="token comment">// [[{ 'user': 'fred',    'age': 40, 'active': true }],[{ 'user': 'barney',  'age': 36, 'active': false }]]</span>
</pre></div><div class="card code-card"><div class="corner advanced"></div><div class="section card-content"><h4 id="permutations">permutations</h4><p>⚠️ <strong>WARNING</strong>: This function's execution time increases exponentially with each array element. Anything more than 8 to 10 entries will cause your browser to hang as it tries to solve all the different combinations.</p><p>Generates all permutations of an array's elements (contains duplicates).</p><p>Use recursion. For each element in the given array, create all the partial permutations for the rest of its elements. Use <code>Array.prototype.map()</code> to combine the element with each partial permutation, then <code>Array.prototype.reduce()</code> to combine all permutations in one array. Base cases are for array <code>length</code> equal to <code>2</code> or <code>1</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">permutations</span> <span class="token operator">=</span> arr <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>arr<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">return</span> arr<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">2</span> <span class="token operator">?</span> <span class="token punctuation">[</span>arr<span class="token punctuation">, [</span>arr<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">],</span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]]] :</span> arr<span class="token punctuation">;</span>
  <span class="token keyword">return</span> arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(
    (</span>acc<span class="token punctuation">,</span> item<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span>
      acc<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>
        <span class="token function">permutations</span><span class="token punctuation">([</span><span class="token operator">...</span>arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> i<span class="token punctuation">),</span> <span class="token operator">...</span>arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)]).</span><span class="token function">map</span><span class="token punctuation">(</span>val <span class="token operator">=></span> <span class="token punctuation">[</span>item<span class="token punctuation">,</span> <span class="token operator">...</span>val<span class="token punctuation">])
      ),
    []
  );
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">permutations</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]);</span> <span class="token comment">// [ [ 1, 33, 5 ], [ 1, 5, 33 ], [ 33, 1, 5 ], [ 33, 5, 1 ], [ 5, 1, 33 ], [ 5, 33, 1 ] ]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="pull">pull</h4><p>Mutates the original array to filter out the values specified.</p><p>Use <code>Array.prototype.filter()</code> and <code>Array.prototype.includes()</code> to pull out the values that are not needed. Use <code>Array.prototype.length = 0</code> to mutate the passed in an array by resetting it's length to zero and <code>Array.prototype.push()</code> to re-populate it with only the pulled values.</p><p><em>(For a snippet that does not mutate the original array see <a href="#without"><code>without</code></a>)</em></p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">pull</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> <span class="token operator">...</span>args<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> argState <span class="token operator">=</span> Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">])</span> <span class="token operator">?</span> args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">] :</span> args<span class="token punctuation">;</span>
  <span class="token keyword">let</span> pulled <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">((</span>v<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=> !</span>argState<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>v<span class="token punctuation">));</span>
  arr<span class="token punctuation">.</span>length <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
  pulled<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>v <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>v<span class="token punctuation">));
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token keyword">let</span> myArray <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">];</span>
<span class="token function">pull</span><span class="token punctuation">(</span>myArray<span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">);</span> <span class="token comment">// myArray = [ 'b', 'b' ]</span>
</pre></div><div class="card code-card"><div class="corner advanced"></div><div class="section card-content"><h4 id="pullatindex">pullAtIndex</h4><p>Mutates the original array to filter out the values at the specified indexes.</p><p>Use <code>Array.prototype.filter()</code> and <code>Array.prototype.includes()</code> to pull out the values that are not needed. Use <code>Array.prototype.length = 0</code> to mutate the passed in an array by resetting it's length to zero and <code>Array.prototype.push()</code> to re-populate it with only the pulled values. Use <code>Array.prototype.push()</code> to keep track of pulled values</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">pullAtIndex</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> pullArr<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> removed <span class="token operator">=</span> <span class="token punctuation">[];</span>
  <span class="token keyword">let</span> pulled <span class="token operator">=</span> arr
    <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">((</span>v<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>pullArr<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token operator">?</span> removed<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>v<span class="token punctuation">) :</span> v<span class="token punctuation">))
    .</span><span class="token function">filter</span><span class="token punctuation">((</span>v<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=> !</span>pullArr<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>i<span class="token punctuation">));</span>
  arr<span class="token punctuation">.</span>length <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
  pulled<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>v <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>v<span class="token punctuation">));</span>
  <span class="token keyword">return</span> removed<span class="token punctuation">;
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token keyword">let</span> myArray <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">];</span>
<span class="token keyword">let</span> pulled <span class="token operator">=</span> <span class="token function">pullAtIndex</span><span class="token punctuation">(</span>myArray<span class="token punctuation">, [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// myArray = [ 'a', 'c' ] , pulled = [ 'b', 'd' ]</span>
</pre></div><div class="card code-card"><div class="corner advanced"></div><div class="section card-content"><h4 id="pullatvalue">pullAtValue</h4><p>Mutates the original array to filter out the values specified. Returns the removed elements.</p><p>Use <code>Array.prototype.filter()</code> and <code>Array.prototype.includes()</code> to pull out the values that are not needed. Use <code>Array.prototype.length = 0</code> to mutate the passed in an array by resetting it's length to zero and <code>Array.prototype.push()</code> to re-populate it with only the pulled values. Use <code>Array.prototype.push()</code> to keep track of pulled values</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">pullAtValue</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> pullArr<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> removed <span class="token operator">=</span> <span class="token punctuation">[],</span>
    pushToRemove <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">((</span>v<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>pullArr<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span> <span class="token operator">?</span> removed<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>v<span class="token punctuation">) :</span> v<span class="token punctuation">)),</span>
    mutateTo <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">((</span>v<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=> !</span>pullArr<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>v<span class="token punctuation">));</span>
  arr<span class="token punctuation">.</span>length <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
  mutateTo<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>v <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>v<span class="token punctuation">));</span>
  <span class="token keyword">return</span> removed<span class="token punctuation">;
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token keyword">let</span> myArray <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">];</span>
<span class="token keyword">let</span> pulled <span class="token operator">=</span> <span class="token function">pullAtValue</span><span class="token punctuation">(</span>myArray<span class="token punctuation">, [</span><span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">]);</span> <span class="token comment">// myArray = [ 'a', 'c' ] , pulled = [ 'b', 'd' ]</span>
</pre></div><div class="card code-card"><div class="corner advanced"></div><div class="section card-content"><h4 id="pullby">pullBy</h4><p>Mutates the original array to filter out the values specified, based on a given iterator function.</p><p>Check if the last argument provided in a function. Use <code>Array.prototype.map()</code> to apply the iterator function <code>fn</code> to all array elements. Use <code>Array.prototype.filter()</code> and <code>Array.prototype.includes()</code> to pull out the values that are not needed. Use <code>Array.prototype.length = 0</code> to mutate the passed in an array by resetting it's length to zero and <code>Array.prototype.push()</code> to re-populate it with only the pulled values.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">pullBy</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> <span class="token operator">...</span>args<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> length <span class="token operator">=</span> args<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
  <span class="token keyword">let</span> fn <span class="token operator">=</span> length <span class="token operator">></span> <span class="token number">1</span> <span class="token operator">?</span> args<span class="token punctuation">[</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">] :</span> undefined<span class="token punctuation">;</span>
  fn <span class="token operator">=</span> <span class="token keyword">typeof</span> fn <span class="token operator">==</span> <span class="token string">'function'</span> <span class="token operator">?</span> <span class="token punctuation">(</span>args<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(),</span> fn<span class="token punctuation">) :</span> undefined<span class="token punctuation">;</span>
  <span class="token keyword">let</span> argState <span class="token operator">=</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">])</span> <span class="token operator">?</span> args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">] :</span> args<span class="token punctuation">).</span><span class="token function">map</span><span class="token punctuation">(</span>val <span class="token operator">=></span> <span class="token function">fn</span><span class="token punctuation">(</span>val<span class="token punctuation">));</span>
  <span class="token keyword">let</span> pulled <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">((</span>v<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=> !</span>argState<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span><span class="token function">fn</span><span class="token punctuation">(</span>v<span class="token punctuation">)));</span>
  arr<span class="token punctuation">.</span>length <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
  pulled<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>v <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>v<span class="token punctuation">));
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token keyword">var</span> myArray <span class="token operator">=</span> <span class="token punctuation">[{</span> x<span class="token punctuation">:</span> <span class="token number">1</span> <span class="token punctuation">}, {</span> x<span class="token punctuation">:</span> <span class="token number">2</span> <span class="token punctuation">}, {</span> x<span class="token punctuation">:</span> <span class="token number">3</span> <span class="token punctuation">}, {</span> x<span class="token punctuation">:</span> <span class="token number">1</span> <span class="token punctuation">}];</span>
<span class="token function">pullBy</span><span class="token punctuation">(</span>myArray<span class="token punctuation">, [{</span> x<span class="token punctuation">:</span> <span class="token number">1</span> <span class="token punctuation">}, {</span> x<span class="token punctuation">:</span> <span class="token number">3</span> <span class="token punctuation">}],</span> o <span class="token operator">=></span> o<span class="token punctuation">.</span>x<span class="token punctuation">);</span> <span class="token comment">// myArray = [{ x: 2 }]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="reducedfilter">reducedFilter</h4><p>Filter an array of objects based on a condition while also filtering out unspecified keys.</p><p>Use <code>Array.prototype.filter()</code> to filter the array based on the predicate <code>fn</code> so that it returns the objects for which the condition returned a truthy value. On the filtered array, use <code>Array.prototype.map()</code> to return the new object using <code>Array.prototype.reduce()</code> to filter out the keys which were not supplied as the <code>keys</code> argument.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">reducedFilter</span> <span class="token operator">=</span> <span class="token punctuation">(</span>data<span class="token punctuation">,</span> keys<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span>
  data<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>fn<span class="token punctuation">).</span><span class="token function">map</span><span class="token punctuation">(</span>el <span class="token operator">=></span>
    keys<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> key<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
      acc<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span class="token operator">=</span> el<span class="token punctuation">[</span>key<span class="token punctuation">];</span>
      <span class="token keyword">return</span> acc<span class="token punctuation">;
    }, {})
  );</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token keyword">const</span> data <span class="token operator">=</span> <span class="token punctuation">[
  {</span>
    id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    name<span class="token punctuation">:</span> <span class="token string">'john'</span><span class="token punctuation">,</span>
    age<span class="token punctuation">:</span> <span class="token number">24</span>
  <span class="token punctuation">},
  {</span>
    id<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
    name<span class="token punctuation">:</span> <span class="token string">'mike'</span><span class="token punctuation">,</span>
    age<span class="token punctuation">:</span> <span class="token number">50</span>
  <span class="token punctuation">}
];</span>

<span class="token function">reducedFilter</span><span class="token punctuation">(</span>data<span class="token punctuation">, [</span><span class="token string">'id'</span><span class="token punctuation">,</span> <span class="token string">'name'</span><span class="token punctuation">],</span> item <span class="token operator">=></span> item<span class="token punctuation">.</span>age <span class="token operator">></span> <span class="token number">24</span><span class="token punctuation">);</span> <span class="token comment">// [{ id: 2, name: 'mike'}]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="reducesuccessive">reduceSuccessive</h4><p>Applies a function against an accumulator and each element in the array (from left to right), returning an array of successively reduced values.</p><p>Use <code>Array.prototype.reduce()</code> to apply the given function to the given array, storing each new result.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">reduceSuccessive</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn<span class="token punctuation">,</span> acc<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>res<span class="token punctuation">,</span> val<span class="token punctuation">,</span> i<span class="token punctuation">,</span> arr<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>res<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token function">fn</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)[</span><span class="token number">0</span><span class="token punctuation">],</span> val<span class="token punctuation">,</span> i<span class="token punctuation">,</span> arr<span class="token punctuation">)),</span> res<span class="token punctuation">), [</span>acc<span class="token punctuation">]);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">reduceSuccessive</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">], (</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token operator">=></span> acc <span class="token operator">+</span> val<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">);</span> <span class="token comment">// [0, 1, 3, 6, 10, 15, 21]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="reducewhich">reduceWhich</h4><p>Returns the minimum/maximum value of an array, after applying the provided function to set comparing rule.</p><p>Use <code>Array.prototype.reduce()</code> in combination with the <code>comparator</code> function to get the appropriate element in the array. You can omit the second parameter, <code>comparator</code>, to use the default one that returns the minimum element in the array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> reduceWhich <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> <span class="token function-variable function">comparator</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> a <span class="token operator">-</span> b<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token function">comparator</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">>=</span> <span class="token number">0</span> <span class="token operator">?</span> b <span class="token punctuation">:</span> a<span class="token punctuation">));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">reduceWhich</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]);</span> <span class="token comment">// 1</span>
<span class="token function">reduceWhich</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">], (</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> b <span class="token operator">-</span> a<span class="token punctuation">);</span> <span class="token comment">// 3</span>
<span class="token function">reduceWhich</span><span class="token punctuation">(
  [{</span> name<span class="token punctuation">:</span> <span class="token string">'Tom'</span><span class="token punctuation">,</span> age<span class="token punctuation">:</span> <span class="token number">12</span> <span class="token punctuation">}, {</span> name<span class="token punctuation">:</span> <span class="token string">'Jack'</span><span class="token punctuation">,</span> age<span class="token punctuation">:</span> <span class="token number">18</span> <span class="token punctuation">}, {</span> name<span class="token punctuation">:</span> <span class="token string">'Lucy'</span><span class="token punctuation">,</span> age<span class="token punctuation">:</span> <span class="token number">9</span> <span class="token punctuation">}],
  (</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> a<span class="token punctuation">.</span>age <span class="token operator">-</span> b<span class="token punctuation">.</span>age
<span class="token punctuation">);</span> <span class="token comment">// {name: "Lucy", age: 9}</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="reject">reject</h4><p>Takes a predicate and array, like <code>Array.prototype.filter()</code>, but only keeps <code>x</code> if <code>pred(x) === false</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">reject</span> <span class="token operator">=</span> <span class="token punctuation">(</span>pred<span class="token punctuation">,</span> array<span class="token punctuation">)</span> <span class="token operator">=></span> array<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">((</span><span class="token operator">...</span>args<span class="token punctuation">)</span> <span class="token operator">=> !</span><span class="token function">pred</span><span class="token punctuation">(</span><span class="token operator">...</span>args<span class="token punctuation">));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">reject</span><span class="token punctuation">(</span>x <span class="token operator">=></span> x <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">===</span> <span class="token number">0</span><span class="token punctuation">, [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]);</span> <span class="token comment">// [1, 3, 5]</span>
<span class="token function">reject</span><span class="token punctuation">(</span>word <span class="token operator">=></span> word<span class="token punctuation">.</span>length <span class="token operator">></span> <span class="token number">4</span><span class="token punctuation">, [</span><span class="token string">'Apple'</span><span class="token punctuation">,</span> <span class="token string">'Pear'</span><span class="token punctuation">,</span> <span class="token string">'Kiwi'</span><span class="token punctuation">,</span> <span class="token string">'Banana'</span><span class="token punctuation">]);</span> <span class="token comment">// ['Pear', 'Kiwi']</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="remove">remove</h4><p>Removes elements from an array for which the given function returns <code>false</code>.</p><p>Use <code>Array.prototype.filter()</code> to find array elements that return truthy values and <code>Array.prototype.reduce()</code> to remove elements using <code>Array.prototype.splice()</code>. The <code>func</code> is invoked with three arguments (<code>value, index, array</code>).</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">remove</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> func<span class="token punctuation">)</span> <span class="token operator">=></span>
  Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>
    <span class="token operator">?</span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>func<span class="token punctuation">).</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
        arr<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>val<span class="token punctuation">),</span> <span class="token number">1</span><span class="token punctuation">);</span>
        <span class="token keyword">return</span> acc<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>val<span class="token punctuation">);
      }, [])
    : [];</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">remove</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">],</span> n <span class="token operator">=></span> n <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">===</span> <span class="token number">0</span><span class="token punctuation">);</span> <span class="token comment">// [2, 4]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="sample">sample</h4><p>Returns a random element from an array.</p><p>Use <code>Math.random()</code> to generate a random number, multiply it by <code>length</code> and round it of to the nearest whole number using <code>Math.floor()</code>. This method also works with strings.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">sample</span> <span class="token operator">=</span> arr <span class="token operator">=></span> arr<span class="token punctuation">[</span>Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">()</span> <span class="token operator">*</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">)];</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">sample</span><span class="token punctuation">([</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">]);</span> <span class="token comment">// 9</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="samplesize">sampleSize</h4><p>Gets <code>n</code> random elements at unique keys from <code>array</code> up to the size of <code>array</code>.</p><p>Shuffle the array using the <a href="https://github.com/30-seconds/30-seconds-of-code#shuffle">Fisher-Yates algorithm</a>. Use <code>Array.prototype.slice()</code> to get the first <code>n</code> elements. Omit the second argument, <code>n</code> to get only one element at random from the array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">sampleSize</span> <span class="token operator">=</span> <span class="token punctuation">([</span><span class="token operator">...</span>arr<span class="token punctuation">],</span> n <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> m <span class="token operator">=</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
  <span class="token keyword">while</span> <span class="token punctuation">(</span>m<span class="token punctuation">) {</span>
    <span class="token keyword">const</span> i <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">()</span> <span class="token operator">*</span> m<span class="token operator">--</span><span class="token punctuation">);
    [</span>arr<span class="token punctuation">[</span>m<span class="token punctuation">],</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">],</span> arr<span class="token punctuation">[</span>m<span class="token punctuation">]];
  }</span>
  <span class="token keyword">return</span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> n<span class="token punctuation">);
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">sampleSize</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [3,1]</span>
<span class="token function">sampleSize</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">4</span><span class="token punctuation">);</span> <span class="token comment">// [2,3,1]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="shank">shank</h4><p>Has the same functionality as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice"><code>Array.prototype.prototype.splice()</code></a>, but returning a new array instead of mutating the original array.</p><p>Use <code>Array.prototype.slice()</code> and <code>Array.prototype.concat()</code> to get a new array with the new contents after removing existing elements and/or adding new elements. Omit the second argument, <code>index</code>, to start at <code>0</code>. Omit the third argument, <code>delCount</code>, to remove <code>0</code> elements. Omit the fourth argument, <code>elements</code>, in order to not add any new elements.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">shank</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> delCount <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">...</span>elements<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr
    <span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> index<span class="token punctuation">)
    .</span><span class="token function">concat</span><span class="token punctuation">(</span>elements<span class="token punctuation">)
    .</span><span class="token function">concat</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>index <span class="token operator">+</span> delCount<span class="token punctuation">));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token keyword">const</span> names <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'alpha'</span><span class="token punctuation">,</span> <span class="token string">'bravo'</span><span class="token punctuation">,</span> <span class="token string">'charlie'</span><span class="token punctuation">];</span>
<span class="token keyword">const</span> namesAndDelta <span class="token operator">=</span> <span class="token function">shank</span><span class="token punctuation">(</span>names<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">'delta'</span><span class="token punctuation">);</span> <span class="token comment">// [ 'alpha', 'delta', 'bravo', 'charlie' ]</span>
<span class="token keyword">const</span> namesNoBravo <span class="token operator">=</span> <span class="token function">shank</span><span class="token punctuation">(</span>names<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">);</span> <span class="token comment">// [ 'alpha', 'charlie' ]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>names<span class="token punctuation">);</span> <span class="token comment">// ['alpha', 'bravo', 'charlie']</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="shuffle">shuffle</h4><p>Randomizes the order of the values of an array, returning a new array.</p><p>Uses the <a href="https://github.com/30-seconds/30-seconds-of-code#shuffle">Fisher-Yates algorithm</a> to reorder the elements of the array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">shuffle</span> <span class="token operator">=</span> <span class="token punctuation">([</span><span class="token operator">...</span>arr<span class="token punctuation">])</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> m <span class="token operator">=</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
  <span class="token keyword">while</span> <span class="token punctuation">(</span>m<span class="token punctuation">) {</span>
    <span class="token keyword">const</span> i <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">()</span> <span class="token operator">*</span> m<span class="token operator">--</span><span class="token punctuation">);
    [</span>arr<span class="token punctuation">[</span>m<span class="token punctuation">],</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">],</span> arr<span class="token punctuation">[</span>m<span class="token punctuation">]];
  }</span>
  <span class="token keyword">return</span> arr<span class="token punctuation">;
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token keyword">const</span> foo <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">];</span>
<span class="token function">shuffle</span><span class="token punctuation">(</span>foo<span class="token punctuation">);</span> <span class="token comment">// [2, 3, 1], foo = [1, 2, 3]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="similarity">similarity</h4><p>Returns an array of elements that appear in both arrays.</p><p>Use <code>Array.prototype.filter()</code> to remove values that are not part of <code>values</code>, determined using <code>Array.prototype.includes()</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">similarity</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> values<span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>v <span class="token operator">=></span> values<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>v<span class="token punctuation">));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">similarity</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">], [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]);</span> <span class="token comment">// [1, 2]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="sortedindex">sortedIndex</h4><p>Returns the lowest index at which value should be inserted into array in order to maintain its sort order.</p><p>Check if the array is sorted in descending order (loosely). Use <code>Array.prototype.findIndex()</code> to find the appropriate index where the element should be inserted.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">sortedIndex</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> isDescending <span class="token operator">=</span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">></span> arr<span class="token punctuation">[</span>arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">];</span>
  <span class="token keyword">const</span> index <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">findIndex</span><span class="token punctuation">(</span>el <span class="token operator">=></span> <span class="token punctuation">(</span>isDescending <span class="token operator">?</span> n <span class="token operator">>=</span> el <span class="token punctuation">:</span> n <span class="token operator">&lt;=</span> el<span class="token punctuation">));</span>
  <span class="token keyword">return</span> index <span class="token operator">=== -</span><span class="token number">1</span> <span class="token operator">?</span> arr<span class="token punctuation">.</span>length <span class="token punctuation">:</span> index<span class="token punctuation">;
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">sortedIndex</span><span class="token punctuation">([</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">],</span> <span class="token number">4</span><span class="token punctuation">);</span> <span class="token comment">// 1</span>
<span class="token function">sortedIndex</span><span class="token punctuation">([</span><span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">],</span> <span class="token number">40</span><span class="token punctuation">);</span> <span class="token comment">// 1</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="sortedindexby">sortedIndexBy</h4><p>Returns the lowest index at which value should be inserted into array in order to maintain its sort order, based on a provided iterator function.</p><p>Check if the array is sorted in descending order (loosely). Use <code>Array.prototype.findIndex()</code> to find the appropriate index where the element should be inserted, based on the iterator function <code>fn</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">sortedIndexBy</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> n<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> isDescending <span class="token operator">=</span> <span class="token function">fn</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">])</span> <span class="token operator">></span> <span class="token function">fn</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]);</span>
  <span class="token keyword">const</span> val <span class="token operator">=</span> <span class="token function">fn</span><span class="token punctuation">(</span>n<span class="token punctuation">);</span>
  <span class="token keyword">const</span> index <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">findIndex</span><span class="token punctuation">(</span>el <span class="token operator">=></span> <span class="token punctuation">(</span>isDescending <span class="token operator">?</span> val <span class="token operator">>=</span> <span class="token function">fn</span><span class="token punctuation">(</span>el<span class="token punctuation">) :</span> val <span class="token operator">&lt;=</span> <span class="token function">fn</span><span class="token punctuation">(</span>el<span class="token punctuation">)));</span>
  <span class="token keyword">return</span> index <span class="token operator">=== -</span><span class="token number">1</span> <span class="token operator">?</span> arr<span class="token punctuation">.</span>length <span class="token punctuation">:</span> index<span class="token punctuation">;
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">sortedIndexBy</span><span class="token punctuation">([{</span> x<span class="token punctuation">:</span> <span class="token number">4</span> <span class="token punctuation">}, {</span> x<span class="token punctuation">:</span> <span class="token number">5</span> <span class="token punctuation">}], {</span> x<span class="token punctuation">:</span> <span class="token number">4</span> <span class="token punctuation">},</span> o <span class="token operator">=></span> o<span class="token punctuation">.</span>x<span class="token punctuation">);</span> <span class="token comment">// 0</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="sortedlastindex">sortedLastIndex</h4><p>Returns the highest index at which value should be inserted into array in order to maintain its sort order.</p><p>Check if the array is sorted in descending order (loosely). Use <code>Array.prototype.reverse()</code> and <code>Array.prototype.findIndex()</code> to find the appropriate last index where the element should be inserted.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">sortedLastIndex</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> isDescending <span class="token operator">=</span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">></span> arr<span class="token punctuation">[</span>arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">];</span>
  <span class="token keyword">const</span> index <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">().</span><span class="token function">findIndex</span><span class="token punctuation">(</span>el <span class="token operator">=></span> <span class="token punctuation">(</span>isDescending <span class="token operator">?</span> n <span class="token operator">&lt;=</span> el <span class="token punctuation">:</span> n <span class="token operator">>=</span> el<span class="token punctuation">));</span>
  <span class="token keyword">return</span> index <span class="token operator">=== -</span><span class="token number">1</span> <span class="token operator">?</span> <span class="token number">0</span> <span class="token punctuation">:</span> arr<span class="token punctuation">.</span>length <span class="token operator">-</span> index<span class="token punctuation">;
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">sortedLastIndex</span><span class="token punctuation">([</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">],</span> <span class="token number">30</span><span class="token punctuation">);</span> <span class="token comment">// 4</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="sortedlastindexby">sortedLastIndexBy</h4><p>Returns the highest index at which value should be inserted into array in order to maintain its sort order, based on a provided iterator function.</p><p>Check if the array is sorted in descending order (loosely). Use <code>Array.prototype.map()</code> to apply the iterator function to all elements of the array. Use <code>Array.prototype.reverse()</code> and <code>Array.prototype.findIndex()</code> to find the appropriate last index where the element should be inserted, based on the provided iterator function.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">sortedLastIndexBy</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> n<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> isDescending <span class="token operator">=</span> <span class="token function">fn</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">])</span> <span class="token operator">></span> <span class="token function">fn</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]);</span>
  <span class="token keyword">const</span> val <span class="token operator">=</span> <span class="token function">fn</span><span class="token punctuation">(</span>n<span class="token punctuation">);</span>
  <span class="token keyword">const</span> index <span class="token operator">=</span> arr
    <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>fn<span class="token punctuation">)
    .</span><span class="token function">reverse</span><span class="token punctuation">()
    .</span><span class="token function">findIndex</span><span class="token punctuation">(</span>el <span class="token operator">=></span> <span class="token punctuation">(</span>isDescending <span class="token operator">?</span> val <span class="token operator">&lt;=</span> el <span class="token punctuation">:</span> val <span class="token operator">>=</span> el<span class="token punctuation">));</span>
  <span class="token keyword">return</span> index <span class="token operator">=== -</span><span class="token number">1</span> <span class="token operator">?</span> <span class="token number">0</span> <span class="token punctuation">:</span> arr<span class="token punctuation">.</span>length <span class="token operator">-</span> index<span class="token punctuation">;
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">sortedLastIndexBy</span><span class="token punctuation">([{</span> x<span class="token punctuation">:</span> <span class="token number">4</span> <span class="token punctuation">}, {</span> x<span class="token punctuation">:</span> <span class="token number">5</span> <span class="token punctuation">}], {</span> x<span class="token punctuation">:</span> <span class="token number">4</span> <span class="token punctuation">},</span> o <span class="token operator">=></span> o<span class="token punctuation">.</span>x<span class="token punctuation">);</span> <span class="token comment">// 1</span>
</pre></div><div class="card code-card"><div class="corner advanced"></div><div class="section card-content"><h4 id="stablesort">stableSort</h4><p>Performs stable sorting of an array, preserving the initial indexes of items when their values are the same. Does not mutate the original array, but returns a new array instead.</p><p>Use <code>Array.prototype.map()</code> to pair each element of the input array with its corresponding index. Use <code>Array.prototype.sort()</code> and a <code>compare</code> function to sort the list, preserving their initial order if the items compared are equal. Use <code>Array.prototype.map()</code> to convert back to the initial array items.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">stableSort</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> compare<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr
    <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">((</span>item<span class="token punctuation">,</span> index<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">({</span> item<span class="token punctuation">,</span> index <span class="token punctuation">}))
    .</span><span class="token function">sort</span><span class="token punctuation">((</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">compare</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>item<span class="token punctuation">,</span> b<span class="token punctuation">.</span>item<span class="token punctuation">)</span> <span class="token operator">||</span> a<span class="token punctuation">.</span>index <span class="token operator">-</span> b<span class="token punctuation">.</span>index<span class="token punctuation">)
    .</span><span class="token function">map</span><span class="token punctuation">(({</span> item <span class="token punctuation">})</span> <span class="token operator">=></span> item<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token keyword">const</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">];</span>
<span class="token keyword">const</span> stable <span class="token operator">=</span> <span class="token function">stableSort</span><span class="token punctuation">(</span>arr<span class="token punctuation">, ()</span> <span class="token operator">=></span> <span class="token number">0</span><span class="token punctuation">);</span> <span class="token comment">// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="symmetricdifference">symmetricDifference</h4><p>Returns the symmetric difference between two arrays, without filtering out duplicate values.</p><p>Create a <code>Set</code> from each array, then use <code>Array.prototype.filter()</code> on each of them to only keep values not contained in the other.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">symmetricDifference</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> sA <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span>a<span class="token punctuation">),</span>
    sB <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span>b<span class="token punctuation">);</span>
  <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token operator">...</span>a<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">=> !</span>sB<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span>x<span class="token punctuation">)),</span> <span class="token operator">...</span>b<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">=> !</span>sA<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span>x<span class="token punctuation">))];
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">symmetricDifference</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">], [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]);</span> <span class="token comment">// [3, 4]</span>
<span class="token function">symmetricDifference</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">], [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]);</span> <span class="token comment">// [2, 2, 3]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="symmetricdifferenceby">symmetricDifferenceBy</h4><p>Returns the symmetric difference between two arrays, after applying the provided function to each array element of both.</p><p>Create a <code>Set</code> by applying <code>fn</code> to each array's elements, then use <code>Array.prototype.filter()</code> on each of them to only keep values not contained in the other.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">symmetricDifferenceBy</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> sA <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>v <span class="token operator">=></span> <span class="token function">fn</span><span class="token punctuation">(</span>v<span class="token punctuation">))),</span>
    sB <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span>b<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>v <span class="token operator">=></span> <span class="token function">fn</span><span class="token punctuation">(</span>v<span class="token punctuation">)));</span>
  <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token operator">...</span>a<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">=> !</span>sB<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token function">fn</span><span class="token punctuation">(</span>x<span class="token punctuation">))),</span> <span class="token operator">...</span>b<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">=> !</span>sA<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token function">fn</span><span class="token punctuation">(</span>x<span class="token punctuation">)))];
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">symmetricDifferenceBy</span><span class="token punctuation">([</span><span class="token number">2.1</span><span class="token punctuation">,</span> <span class="token number">1.2</span><span class="token punctuation">], [</span><span class="token number">2.3</span><span class="token punctuation">,</span> <span class="token number">3.4</span><span class="token punctuation">],</span> Math<span class="token punctuation">.</span>floor<span class="token punctuation">);</span> <span class="token comment">// [ 1.2, 3.4 ]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="symmetricdifferencewith">symmetricDifferenceWith</h4><p>Returns the symmetric difference between two arrays, using a provided function as a comparator.</p><p>Use <code>Array.prototype.filter()</code> and <code>Array.prototype.findIndex()</code> to find the appropriate values.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">symmetricDifferenceWith</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> val<span class="token punctuation">,</span> comp<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">[</span>
  <span class="token operator">...</span>arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>a <span class="token operator">=></span> val<span class="token punctuation">.</span><span class="token function">findIndex</span><span class="token punctuation">(</span>b <span class="token operator">=></span> <span class="token function">comp</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">))</span> <span class="token operator">=== -</span><span class="token number">1</span><span class="token punctuation">),</span>
  <span class="token operator">...</span>val<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>a <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">findIndex</span><span class="token punctuation">(</span>b <span class="token operator">=></span> <span class="token function">comp</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">))</span> <span class="token operator">=== -</span><span class="token number">1</span><span class="token punctuation">)
];</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">symmetricDifferenceWith</span><span class="token punctuation">(
  [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1.2</span><span class="token punctuation">,</span> <span class="token number">1.5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">],
  [</span><span class="token number">1.9</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">3.9</span><span class="token punctuation">],
  (</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token operator">===</span> Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span>b<span class="token punctuation">)
);</span> <span class="token comment">// [1, 1.2, 3.9]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="tail">tail</h4><p>Returns all elements in an array except for the first one.</p><p>Return <code>Array.prototype.slice(1)</code> if the array's <code>length</code> is more than <code>1</code>, otherwise, return the whole array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">tail</span> <span class="token operator">=</span> arr <span class="token operator">=></span> <span class="token punctuation">(</span>arr<span class="token punctuation">.</span>length <span class="token operator">></span> <span class="token number">1</span> <span class="token operator">?</span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">) :</span> arr<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">tail</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// [2,3]</span>
<span class="token function">tail</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">]);</span> <span class="token comment">// [1]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="take">take</h4><p>Returns an array with n elements removed from the beginning.</p><p>Use <code>Array.prototype.slice()</code> to create a slice of the array with <code>n</code> elements taken from the beginning.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">take</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> n <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> n<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">take</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">5</span><span class="token punctuation">);</span> <span class="token comment">// [1, 2, 3]</span>
<span class="token function">take</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">0</span><span class="token punctuation">);</span> <span class="token comment">// []</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="takeright">takeRight</h4><p>Returns an array with n elements removed from the end.</p><p>Use <code>Array.prototype.slice()</code> to create a slice of the array with <code>n</code> elements taken from the end.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">takeRight</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> n <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span>length <span class="token operator">-</span> n<span class="token punctuation">,</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">takeRight</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [ 2, 3 ]</span>
<span class="token function">takeRight</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// [3]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="takerightwhile">takeRightWhile</h4><p>Removes elements from the end of an array until the passed function returns <code>true</code>. Returns the removed elements.</p><p>Loop through the array, using a <code>Array.prototype.reduceRight()</code> and accumulating elements while the function returns falsy value.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">takeRightWhile</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> func<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">reduceRight</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> el<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token function">func</span><span class="token punctuation">(</span>el<span class="token punctuation">)</span> <span class="token operator">?</span> acc <span class="token punctuation">: [</span>el<span class="token punctuation">,</span> <span class="token operator">...</span>acc<span class="token punctuation">]), []);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">takeRightWhile</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">],</span> n <span class="token operator">=></span> n <span class="token operator">&lt;</span> <span class="token number">3</span><span class="token punctuation">);</span> <span class="token comment">// [3, 4]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="takewhile">takeWhile</h4><p>Removes elements in an array until the passed function returns <code>true</code>. Returns the removed elements.</p><p>Loop through the array, using a <code>for...of</code> loop over <code>Array.prototype.entries()</code> until the returned value from the function is <code>true</code>. Return the removed elements, using <code>Array.prototype.slice()</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">takeWhile</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> func<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token punctuation">[</span>i<span class="token punctuation">,</span> val<span class="token punctuation">]</span> <span class="token keyword">of</span> arr<span class="token punctuation">.</span><span class="token function">entries</span><span class="token punctuation">())</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">func</span><span class="token punctuation">(</span>val<span class="token punctuation">))</span> <span class="token keyword">return</span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> i<span class="token punctuation">);</span>
  <span class="token keyword">return</span> arr<span class="token punctuation">;
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">takeWhile</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">],</span> n <span class="token operator">=></span> n <span class="token operator">>=</span> <span class="token number">3</span><span class="token punctuation">);</span> <span class="token comment">// [1, 2]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="tohash">toHash</h4><p>Reduces a given Array-like into a value hash (keyed data store).</p><p>Given an Iterable or Array-like structure, call <code>Array.prototype.prototype.reduce.call()</code> on the provided object to step over it and return an Object, keyed by the reference value.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">toHash</span> <span class="token operator">=</span> <span class="token punctuation">(</span>object<span class="token punctuation">,</span> key<span class="token punctuation">)</span> <span class="token operator">=></span>
  Array<span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>reduce<span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>
    object<span class="token punctuation">,
    (</span>acc<span class="token punctuation">,</span> data<span class="token punctuation">,</span> index<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">((</span>acc<span class="token punctuation">[</span><span class="token operator">!</span>key <span class="token operator">?</span> index <span class="token punctuation">:</span> data<span class="token punctuation">[</span>key<span class="token punctuation">]]</span> <span class="token operator">=</span> data<span class="token punctuation">),</span> acc<span class="token punctuation">),
    {}
  );</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">toHash</span><span class="token punctuation">([</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]);</span> <span class="token comment">// { 0: 4, 1: 3, 2: 2, 1: 1 }</span>
<span class="token function">toHash</span><span class="token punctuation">([{</span> a<span class="token punctuation">:</span> <span class="token string">'label'</span> <span class="token punctuation">}],</span> <span class="token string">'a'</span><span class="token punctuation">);</span> <span class="token comment">// { label: { a: 'label' } }</span>
<span class="token comment">// A more in depth example:</span>
<span class="token keyword">let</span> users <span class="token operator">=</span> <span class="token punctuation">[{</span> id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> first<span class="token punctuation">:</span> <span class="token string">'Jon'</span> <span class="token punctuation">}, {</span> id<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span> first<span class="token punctuation">:</span> <span class="token string">'Joe'</span> <span class="token punctuation">}, {</span> id<span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">,</span> first<span class="token punctuation">:</span> <span class="token string">'Moe'</span> <span class="token punctuation">}];</span>
<span class="token keyword">let</span> managers <span class="token operator">=</span> <span class="token punctuation">[{</span> manager<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> employees<span class="token punctuation">: [</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">] }];</span>
<span class="token comment">// We use function here because we want a bindable reference, but a closure referencing the hash would work, too.</span>
managers<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>
  manager <span class="token operator">=></span>
    <span class="token punctuation">(</span>manager<span class="token punctuation">.</span>employees <span class="token operator">=</span> manager<span class="token punctuation">.</span>employees<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span>id<span class="token punctuation">) {</span>
      <span class="token keyword">return this</span><span class="token punctuation">[</span>id<span class="token punctuation">];
    },</span> <span class="token function">toHash</span><span class="token punctuation">(</span>users<span class="token punctuation">,</span> <span class="token string">'id'</span><span class="token punctuation">)))
);</span>
managers<span class="token punctuation">;</span> <span class="token comment">// [ { manager:1, employees: [ { id: 2, first: "Joe" }, { id: 3, first: "Moe" } ] } ]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="union">union</h4><p>Returns every element that exists in any of the two arrays once.</p><p>Create a <code>Set</code> with all values of <code>a</code> and <code>b</code> and convert to an array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">union</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">([</span><span class="token operator">...</span>a<span class="token punctuation">,</span> <span class="token operator">...</span>b<span class="token punctuation">]));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">union</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">], [</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]);</span> <span class="token comment">// [1,2,3,4]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="unionby">unionBy</h4><p>Returns every element that exists in any of the two arrays once, after applying the provided function to each array element of both.</p><p>Create a <code>Set</code> by applying all <code>fn</code> to all values of <code>a</code>. Create a <code>Set</code> from <code>a</code> and all elements in <code>b</code> whose value, after applying <code>fn</code> does not match a value in the previously created set. Return the last set converted to an array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">unionBy</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> s <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>v <span class="token operator">=></span> <span class="token function">fn</span><span class="token punctuation">(</span>v<span class="token punctuation">)));</span>
  <span class="token keyword">return</span> Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">([</span><span class="token operator">...</span>a<span class="token punctuation">,</span> <span class="token operator">...</span>b<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">=> !</span>s<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token function">fn</span><span class="token punctuation">(</span>x<span class="token punctuation">)))]));
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">unionBy</span><span class="token punctuation">([</span><span class="token number">2.1</span><span class="token punctuation">], [</span><span class="token number">1.2</span><span class="token punctuation">,</span> <span class="token number">2.3</span><span class="token punctuation">],</span> Math<span class="token punctuation">.</span>floor<span class="token punctuation">);</span> <span class="token comment">// [2.1, 1.2]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="unionwith">unionWith</h4><p>Returns every element that exists in any of the two arrays once, using a provided comparator function.</p><p>Create a <code>Set</code> with all values of <code>a</code> and values in <code>b</code> for which the comparator finds no matches in <code>a</code>, using <code>Array.prototype.findIndex()</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">unionWith</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> comp<span class="token punctuation">)</span> <span class="token operator">=></span>
  Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">([</span><span class="token operator">...</span>a<span class="token punctuation">,</span> <span class="token operator">...</span>b<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">=></span> a<span class="token punctuation">.</span><span class="token function">findIndex</span><span class="token punctuation">(</span>y <span class="token operator">=></span> <span class="token function">comp</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">))</span> <span class="token operator">=== -</span><span class="token number">1</span><span class="token punctuation">)]));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">unionWith</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1.2</span><span class="token punctuation">,</span> <span class="token number">1.5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">], [</span><span class="token number">1.9</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">3.9</span><span class="token punctuation">], (</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token operator">===</span> Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span>b<span class="token punctuation">));</span> <span class="token comment">// [1, 1.2, 1.5, 3, 0, 3.9]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="uniqueelements">uniqueElements</h4><p>Returns all unique values of an array.</p><p>Use ES6 <code>Set</code> and the <code>...rest</code> operator to discard all duplicated values.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">uniqueElements</span> <span class="token operator">=</span> arr <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span>arr<span class="token punctuation">)];</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">uniqueElements</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]);</span> <span class="token comment">// [1, 2, 3, 4, 5]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="uniqueelementsby">uniqueElementsBy</h4><p>Returns all unique values of an array, based on a provided comparator function.</p><p>Use <code>Array.prototype.reduce()</code> and <code>Array.prototype.some()</code> for an array containing only the first unique occurence of each value, based on the comparator function, <code>fn</code>. The comparator function takes two arguments: the values of the two elements being compared.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">uniqueElementsBy</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> v<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>acc<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span>x <span class="token operator">=></span> <span class="token function">fn</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span> x<span class="token punctuation">)))</span> acc<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>v<span class="token punctuation">);</span>
    <span class="token keyword">return</span> acc<span class="token punctuation">;
  }, []);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">uniqueElementsBy</span><span class="token punctuation">(
  [
    {</span> id<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'a'</span> <span class="token punctuation">},
    {</span> id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'b'</span> <span class="token punctuation">},
    {</span> id<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'c'</span> <span class="token punctuation">},
    {</span> id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'d'</span> <span class="token punctuation">},
    {</span> id<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'e'</span> <span class="token punctuation">}
  ],
  (</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> a<span class="token punctuation">.</span>id <span class="token operator">==</span> b<span class="token punctuation">.</span>id
<span class="token punctuation">);</span> <span class="token comment">// [ { id: 0, value: 'a' }, { id: 1, value: 'b' }, { id: 2, value: 'c' } ]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="uniqueelementsbyright">uniqueElementsByRight</h4><p>Returns all unique values of an array, based on a provided comparator function.</p><p>Use <code>Array.prototype.reduce()</code> and <code>Array.prototype.some()</code> for an array containing only the last unique occurence of each value, based on the comparator function, <code>fn</code>. The comparator function takes two arguments: the values of the two elements being compared.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">uniqueElementsByRight</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">reduceRight</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> v<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>acc<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span>x <span class="token operator">=></span> <span class="token function">fn</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span> x<span class="token punctuation">)))</span> acc<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>v<span class="token punctuation">);</span>
    <span class="token keyword">return</span> acc<span class="token punctuation">;
  }, []);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">uniqueElementsByRight</span><span class="token punctuation">(
  [
    {</span> id<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'a'</span> <span class="token punctuation">},
    {</span> id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'b'</span> <span class="token punctuation">},
    {</span> id<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'c'</span> <span class="token punctuation">},
    {</span> id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'d'</span> <span class="token punctuation">},
    {</span> id<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'e'</span> <span class="token punctuation">}
  ],
  (</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> a<span class="token punctuation">.</span>id <span class="token operator">==</span> b<span class="token punctuation">.</span>id
<span class="token punctuation">);</span> <span class="token comment">// [ { id: 0, value: 'e' }, { id: 1, value: 'd' }, { id: 2, value: 'c' } ]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="uniquesymmetricdifference">uniqueSymmetricDifference</h4><p>Returns the unique symmetric difference between two arrays, not containing duplicate values from either array.</p><p>Use <code>Array.prototype.filter()</code> and <code>Array.prototype.includes()</code> on each array to remove values contained in the other, then create a <code>Set</code> from the results, removing duplicate values.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">uniqueSymmetricDifference</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">[</span>
  <span class="token operator">...</span><span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">([</span><span class="token operator">...</span>a<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>v <span class="token operator">=> !</span>b<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>v<span class="token punctuation">)),</span> <span class="token operator">...</span>b<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>v <span class="token operator">=> !</span>a<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>v<span class="token punctuation">))])
];</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">uniqueSymmetricDifference</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">], [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]);</span> <span class="token comment">// [3, 4]</span>
<span class="token function">uniqueSymmetricDifference</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">], [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]);</span> <span class="token comment">// [2, 3]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="unzip">unzip</h4><p>Creates an array of arrays, ungrouping the elements in an array produced by <a href="#zip">zip</a>.</p><p>Use <code>Math.max.apply()</code> to get the longest subarray in the array, <code>Array.prototype.map()</code> to make each element an array. Use <code>Array.prototype.reduce()</code> and <code>Array.prototype.forEach()</code> to map grouped values to individual arrays.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">unzip</span> <span class="token operator">=</span> arr <span class="token operator">=></span>
  arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(
    (</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>val<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">((</span>v<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> acc<span class="token punctuation">[</span>i<span class="token punctuation">].</span><span class="token function">push</span><span class="token punctuation">(</span>v<span class="token punctuation">)),</span> acc<span class="token punctuation">),</span>
    Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">({</span>
      length<span class="token punctuation">:</span> Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token operator">...</span>arr<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>x <span class="token operator">=></span> x<span class="token punctuation">.</span>length<span class="token punctuation">))
    }).</span><span class="token function">map</span><span class="token punctuation">(</span>x <span class="token operator">=></span> <span class="token punctuation">[])
  );</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">unzip</span><span class="token punctuation">([[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">], [</span><span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">]]);</span> <span class="token comment">//[['a', 'b'], [1, 2], [true, false]]</span>
<span class="token function">unzip</span><span class="token punctuation">([[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">], [</span><span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]]);</span> <span class="token comment">//[['a', 'b'], [1, 2], [true]]</span>
</pre></div><div class="card code-card"><div class="corner advanced"></div><div class="section card-content"><h4 id="unzipwith">unzipWith</h4><p>Creates an array of elements, ungrouping the elements in an array produced by <a href="#zip">zip</a> and applying the provided function.</p><p>Use <code>Math.max.apply()</code> to get the longest subarray in the array, <code>Array.prototype.map()</code> to make each element an array. Use <code>Array.prototype.reduce()</code> and <code>Array.prototype.forEach()</code> to map grouped values to individual arrays. Use <code>Array.prototype.map()</code> and the spread operator (<code>...</code>) to apply <code>fn</code> to each individual group of elements.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">unzipWith</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> fn<span class="token punctuation">)</span> <span class="token operator">=></span>
  arr
    <span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(
      (</span>acc<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>val<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">((</span>v<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> acc<span class="token punctuation">[</span>i<span class="token punctuation">].</span><span class="token function">push</span><span class="token punctuation">(</span>v<span class="token punctuation">)),</span> acc<span class="token punctuation">),</span>
      Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">({</span>
        length<span class="token punctuation">:</span> Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token operator">...</span>arr<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>x <span class="token operator">=></span> x<span class="token punctuation">.</span>length<span class="token punctuation">))
      }).</span><span class="token function">map</span><span class="token punctuation">(</span>x <span class="token operator">=></span> <span class="token punctuation">[])
    )
    .</span><span class="token function">map</span><span class="token punctuation">(</span>val <span class="token operator">=></span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token operator">...</span>val<span class="token punctuation">));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">unzipWith</span><span class="token punctuation">([[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">], [</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">]], (</span><span class="token operator">...</span>args<span class="token punctuation">)</span> <span class="token operator">=></span> args<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> v<span class="token punctuation">)</span> <span class="token operator">=></span> acc <span class="token operator">+</span> v<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">));</span> <span class="token comment">// [3, 30, 300]</span>
</pre></div><div class="card code-card"><div class="corner beginner"></div><div class="section card-content"><h4 id="without">without</h4><p>Filters out the elements of an array, that have one of the specified values.</p><p>Use <code>Array.prototype.filter()</code> to create an array excluding(using <code>!Array.includes()</code>) all given values.</p><p><em>(For a snippet that mutates the original array see <a href="#pull"><code>pull</code></a>)</em></p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">without</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> <span class="token operator">...</span>args<span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>v <span class="token operator">=> !</span>args<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>v<span class="token punctuation">));</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">without</span><span class="token punctuation">([</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">);</span> <span class="token comment">// [3]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="xprod">xProd</h4><p>Creates a new array out of the two supplied by creating each possible pair from the arrays.</p><p>Use <code>Array.prototype.reduce()</code>, <code>Array.prototype.map()</code> and <code>Array.prototype.concat()</code> to produce every possible pair from the elements of the two arrays and save them in an array.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">xProd</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=></span> a<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> x<span class="token punctuation">)</span> <span class="token operator">=></span> acc<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>b<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>y <span class="token operator">=></span> <span class="token punctuation">[</span>x<span class="token punctuation">,</span> y<span class="token punctuation">])), []);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">xProd</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">], [</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">]);</span> <span class="token comment">// [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="zip">zip</h4><p>Creates an array of elements, grouped based on the position in the original arrays.</p><p>Use <code>Math.max.apply()</code> to get the longest array in the arguments. Creates an array with that length as return value and use <code>Array.from()</code> with a map-function to create an array of grouped elements. If lengths of the argument-arrays vary, <code>undefined</code> is used where no value could be found.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">zip</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">...</span>arrays<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> maxLength <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token operator">...</span>arrays<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>x <span class="token operator">=></span> x<span class="token punctuation">.</span>length<span class="token punctuation">));</span>
  <span class="token keyword">return</span> Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">({</span> length<span class="token punctuation">:</span> maxLength <span class="token punctuation">}).</span><span class="token function">map</span><span class="token punctuation">((</span>_<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">({</span> length<span class="token punctuation">:</span> arrays<span class="token punctuation">.</span>length <span class="token punctuation">}, (</span>_<span class="token punctuation">,</span> k<span class="token punctuation">)</span> <span class="token operator">=></span> arrays<span class="token punctuation">[</span>k<span class="token punctuation">][</span>i<span class="token punctuation">]);
  });
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">zip</span><span class="token punctuation">([</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">], [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">], [</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">]);</span> <span class="token comment">// [['a', 1, true], ['b', 2, false]]</span>
<span class="token function">zip</span><span class="token punctuation">([</span><span class="token string">'a'</span><span class="token punctuation">], [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">], [</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">]);</span> <span class="token comment">// [['a', 1, true], [undefined, 2, false]]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="zipobject">zipObject</h4><p>Given an array of valid property identifiers and an array of values, return an object associating the properties to the values.</p><p>Since an object can have undefined values but not undefined property pointers, the array of properties is used to decide the structure of the resulting object using <code>Array.prototype.reduce()</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">zipObject</span> <span class="token operator">=</span> <span class="token punctuation">(</span>props<span class="token punctuation">,</span> values<span class="token punctuation">)</span> <span class="token operator">=></span>
  props<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>obj<span class="token punctuation">,</span> prop<span class="token punctuation">,</span> index<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">((</span>obj<span class="token punctuation">[</span>prop<span class="token punctuation">]</span> <span class="token operator">=</span> values<span class="token punctuation">[</span>index<span class="token punctuation">]),</span> obj<span class="token punctuation">), {});</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">zipObject</span><span class="token punctuation">([</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">], [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]);</span> <span class="token comment">// {a: 1, b: 2, c: undefined}</span>
<span class="token function">zipObject</span><span class="token punctuation">([</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">], [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// {a: 1, b: 2}</span>
</pre></div><div class="card code-card"><div class="corner advanced"></div><div class="section card-content"><h4 id="zipwith">zipWith</h4><p>Creates an array of elements, grouped based on the position in the original arrays and using function as the last value to specify how grouped values should be combined.</p><p>Check if the last argument provided is a function. Use <code>Math.max()</code> to get the longest array in the arguments. Creates an array with that length as return value and use <code>Array.from()</code> with a map-function to create an array of grouped elements. If lengths of the argument-arrays vary, <code>undefined</code> is used where no value could be found. The function is invoked with the elements of each group <code>(...group)</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">zipWith</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">...</span>array<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> fn <span class="token operator">=</span> <span class="token keyword">typeof</span> array<span class="token punctuation">[</span>array<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token string">'function'</span> <span class="token operator">?</span> array<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">() :</span> undefined<span class="token punctuation">;</span>
  <span class="token keyword">return</span> Array<span class="token punctuation">.</span><span class="token keyword">from</span><span class="token punctuation">(
    {</span> length<span class="token punctuation">:</span> Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token operator">...</span>array<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>a <span class="token operator">=></span> a<span class="token punctuation">.</span>length<span class="token punctuation">)) },
    (</span>_<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>fn <span class="token operator">?</span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token operator">...</span>array<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>a <span class="token operator">=></span> a<span class="token punctuation">[</span>i<span class="token punctuation">])) :</span> array<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>a <span class="token operator">=></span> a<span class="token punctuation">[</span>i<span class="token punctuation">]))
  );
};</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">zipWith</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">], [</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">], [</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">], (</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token punctuation">)</span> <span class="token operator">=></span> a <span class="token operator">+</span> b <span class="token operator">+</span> c<span class="token punctuation">);</span> <span class="token comment">// [111,222]</span>
<span class="token function">zipWith</span><span class="token punctuation">(
  [</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],
  [</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">],
  [</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">],
  (</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>a <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">?</span> a <span class="token punctuation">:</span> <span class="token string">'a'</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token punctuation">(</span>b <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">?</span> b <span class="token punctuation">:</span> <span class="token string">'b'</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token punctuation">(</span>c <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">?</span> c <span class="token punctuation">:</span> <span class="token string">'c'</span><span class="token punctuation">)
);</span> <span class="token comment">// [111, 222, '3bc']</span>
</pre></div></main><footer class="col-full-width container"><div class="col-centered"><p style="display:inline-block"><strong>30 seconds of code</strong> is licensed under the <a href="https://github.com/30-seconds/30-seconds-of-code/blob/master/LICENSE">CC0-1.0</a> license.<br>Logos made by <a href="https://github.com/Chalarangelo">Angelos Chalaris</a> and ribbon made by <a href="https://github.com/tholman/github-corners">Tim Holman</a> are licensed under the <a href="https://opensource.org/licenses/MIT">MIT</a> license.<br>Sponsored by <img src="https://30secondsofcode.org/sponsors/DO_Logo_icon_blue.svg" style="vertical-align:sub;padding-right:2px;padding-left:2px" width="20px" height="20px"><a href="https://www.digitalocean.com" alt="DigitalOcean"><span style="color:#0080ff">DigitalOcean</span></a>.</p><br/><p style="display:inline-block"><a href="./about">About</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="./contributing">Contributing</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="./archive">Archive</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="./glossary">Glossary</a></p></div></footer><a class="scroll-to-top" href="#top"></a></div></body></html>